This article is part of the Linux Shell Series Tutorial (10), more Linux shell tutorials: Linux Shell Tutorials
Basically any language has its own looping statements, of course, the shell is no exception, today we introduce the use of shell for loop.
Shell for Loop syntax
The syntax for the Shell for loop is as follows
For variable in list do Command1 command2 ... Commandndone
A list is a sequence of values (numbers, strings, and so on), each separated by a space. Once per loop, the values in the list are placed sequentially into the specified variable, and then the command area is repeated (between do and done) until all elements have been exhausted.
Shell for Loop Example
The following example shows you how to use the shell for loop.
For loop in one II tree Fourdo echo "I am: $loop" done
Output Result:
I am:onei Am:twoi am:treei am:four
You can sequentially output a string of words, example:
For STR in I am Linuxdaxuedo Echo $strdone
Output Result:
Iamlinuxdaxue
You can display the files in the current directory, for example:
For file in./*do Echo $filedone
Output Result:
./a.sh./automake./automonitor./crzk./makedir./shell_start./sql./submit
The shell for loop command is easier to master, so let's introduce it to you today. For more shell tutorials See: Linux Shell Series Tutorials
This article by Linux technology talent "Daxue" published in: Linux University
This article fixed link: Linux Shell series Tutorial (10) Shell for Loop
Linux Shell Series Tutorial (10) Shell for Loop