DEV Community

Cover image for For Loop | Shell Scripting
Rahul Mishra
Rahul Mishra

Posted on • Originally published at programmingport.hashnode.dev

For Loop | Shell Scripting

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
Enter fullscreen mode Exit fullscreen mode
    ```shell
    for <variable_name> in file1 file2 file3
    do
        command1 on $<variable_name>
        command2
        commandN
    done
    ```
Enter fullscreen mode Exit fullscreen mode
- Third way
Enter fullscreen mode Exit fullscreen mode
    ```shell
    for <variable_name> in $(Linux-Or-Unix_Command_Here)
    do
        command1 on $<variable_name>
        command2 on $<variable_name>
        commandN
    done
    ```
Enter fullscreen mode Exit fullscreen mode
- Forth way
Enter fullscreen mode Exit fullscreen mode
    ```shell
    for (( EXP1; EXP2; EXP3 ))
    do
        command1
        command2
        command3
    done
    ```
Enter fullscreen mode Exit fullscreen mode
  1. We can give the range to iterate like this {1..10}. Here the range is from 1 to 10
  2. 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
  3. 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

🚀 Other links

GitHub
HackerRank

Oldest comments (0)