For Loop | Shell Scripting

For Loop | Shell Scripting

ยท

3 min read

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.

  1. 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
        for <variable_name> in file1 file2 file3
        do
            command1 on $<variable_name>
            command2
            commandN
        done
      
    • Third way
        for <variable_name> in $(Linux-Or-Unix_Command_Here)
        do
            command1 on $<variable_name>
            command2 on $<variable_name>
            commandN
        done
      
    • Forth way
        for (( EXP1; EXP2; EXP3 ))
        do
            command1
            command2
            command3
        done
      
  2. We can give the range to iterate like this {1..10}. Here the range is from 1 to 10
  3. If we want to specify increment than we can do that in this way {1..10..2}. Here the range is from 1 to 10 and increment will be of 2
  4. You can use the above methods only if your bash version is above 4.0. You can check you bash version by this command echo ${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,

๐Ÿ“ง Write a mail

rahulmishra102000@gmail.com

GitHub, HackerRank