This is a multipart blog article series where I am going to explain the concepts of shell scripting and how to write a shell script in Linux, UNIX or Mac based systems. You can also follow this tutorial blog using windows but for that you have to install a bash from.
In this article we are going to understand for loop in shell scripting.
-
The syntax for loop. There are many ways to write the for loop.
-
First way
for <variable_name> in 1 2 3 4 5 . . N do command1 command2 command3 done
-
- Second way
```shell
for <variable_name> in file1 file2 file3
do
command1 on $<variable_name>
command2
commandN
done
```
- Third way
```shell
for <variable_name> in $(Linux-Or-Unix_Command_Here)
do
command1 on $<variable_name>
command2 on $<variable_name>
commandN
done
```
- Forth way
```shell
for (( EXP1; EXP2; EXP3 ))
do
command1
command2
command3
done
```
- We can give the range to iterate like this
{1..10}
. Here the range is from1
to10
- If we want to specify increment than we can do that in this way
{1..10..2}
. Here the range is from1
to10
and increment will be of2
- You can use the above methods only if your bash version is above
4.0
. You can check you bash version by this commandecho ${BASH_VERSION}
Reference code file for this article
So this was a brief introduction of for loop. Hope you liked it and learned something new form it.
If you have any doubt, question, quires related to this topic or just want to share something with me, than please feel free to contact me.
📱 Contact Me
Twitter
LinkedIn
Telegram
Instagram
Top comments (0)