This article is the tenth part of the Linux Shell Scripting Series . For more Linux Shell tutorials, please see: Linux Shell Scripting Series Tutorials
Basically any language has its own loop statement, and Shell is no exception. Following the previous article , today I will introduce the usage of Shell for loop.
Shell for loop syntax
The syntax of the shell for loop is shown below
for variable in list do command1 command2 ... commandN done
A list is a sequence of values (numbers, strings, etc.), each value separated by a space. Each time through the loop, the values in the list are sequentially placed into the specified variables, and then the command area (between do and done) is repeated until all elements are exhausted.
Shell for loop example
Next, I will introduce the usage of Shell for loop by way of example.
for loop in one two tree four do echo "I am : $loop" done
Output result:
I am : one I am : two I am : tree I am : four
The words of the string can be output sequentially, for example:
for str in I am justcode.ikeepstudying do echo $str done
Output result:
I am justcode.ikeepstudying
You can display files in the current directory, for example:
for file in ./* do echo $file done
Output result:
./a.sh ./automake ./automonitor ./crzk ./make ./shell_start ./sql ./submit
The Shell for loop command is relatively easy to master, so I will introduce it to you today. For more Shell tutorials, please see: Linux Shell Scripting Series Tutorials
Original: Linux Shell series of tutorials (ten) Shell for loop
This article is transferred from: Linux Shell Scripting Tutorial Series (10) Shell for loop