Bash For Loop usage guide for absolute beginners


Shell Scripting

I get this question a lot from many users, how can I run a certain command in a loop for a defined period of time? Or how can we iterate a task over a range only for n number of times. All these can be achieved using bash for loop in Linux and Unix

From What is Loop, a loop is a sequence of instructions that is continually repeated until a certain condition is reached. So we know that a loop is a situation where we can perform a certain task repeatedly for a certain pre-defined period of time or may be some times infinite.

Using such loops in our day to day task are very useful to automate stuffs using scripts. The loop can be configured using for, while, until etc depending upon individual's requirement. In this tutorial we will understand in detail about bash for loop, and it's usage across Linux environment for different types of automation shell scripts.

I will also share different shell script examples using for loop to help you understand it's usage.

 

Basic for loop syntax in Bash

The syntax of for loop would vary based on the programming language you choose such as C, perl, python, go etc. The provided syntax can be used only with bash and shell scripts

for {ELEMENT} in ${ARRAY[@]}
do
    {COMMAND}
done

 

Understanding the syntax

  • Here the ARRAY can contain any type of element such as strings, integers.
  • Although it is important that you pass an ARRAY to the loop, as if you pass a VARIABLE then the loop with count all the ELEMENTS from the VARIABLE as single ELEMENT. I will show you what this means later in this article
  • Loop will iterate each ELEMENT from the ARRAY and a certain command can be assigned to these ELEMENT
  • The loop will run until the last ELEMENT from ARRAY

 

EX_1: Loop over a range of numbers

In our first shell script we will iterate over a range of numbers and print the number

for num in 1 2 3 4 5 6
do
   echo "Found Element: $num"
done

Here we have passed a series of numbers separated by white space

The output from this script

Found Element: 1
Found Element: 2
Found Element: 3
Found Element: 4
Found Element: 5
Found Element: 6

 

EX_2: Loop over a series of strings

In this shell script we will iterate over a series of strings

for string in STRING1 STRING2 STRING3
do
   echo "Found Element: $string"
done

The output from this script:

Found Element: STRING1
Found Element: STRING2
Found Element: STRING3

Let us also take a practical example for Linux Administrator. In this script we will write a small script which will search for file1, file2 and file3 under /tmp
Now if you are not using for loop then you have to manually search for these files. You may think, so what it is easier to find files manually?

Yes, TRUE but what if you have 100 files or 1000 files? Would you find them manually?

for file in file1 file2 file3
do
   echo "Searching for file: $file"
   file_path=`find /tmp -name $file`
   if [ ! -z $file_path ];then
      echo "Found: $file at $file_path"
   else
      echo "Not Found: $file"
   fi
done

The output from the script will tell us the files which were found and the files which were not found.

Beginners Tutorial to use Bash For Loop
Bash for loop in a range

 

EX_3: Use for loop with an array

Before we go ahead it is important that you understand the different between ARRAY and Variable

Array vs Variable

  • It is important that when you use loop, you use an ARRAY which contains elements separated by white character
  • A variable will always contain a single element while an array can contain multiple number of elements
  • An array can also store multiple variables
  • You can convert your VARIABLE into an ARRAY using different methods.

For example:

VAR="My name is Deepak"

Here we have given white space separated values, so does this become an ARRAY? NO, this is a VARIABLE

Check the length of this VARIABLE

echo "Length: ${#VAR[@]}"
Length: 1

Bash could only count single element here, so if we try to iterate using this VAR then we get only 1 iteration

You should know how to declare an array in shell script:

Now I will declare the same content as an array

VAR=(My name is Deepak)

Here I have added the elements inside parenthesis, now let us check the length of this variable

# echo "Length: ${#VAR[@]}"
Length: 4

So now bash considers this as an ARRAY with 4 ELEMENTS

1st - My
2nd - name
3rd - is
4th - Deepak

We can access these individual elements using ${VAR[$num]}

Here $num should be replaced by the element number you want to access, for example:

#!/bin/bash

VAR=(My name is Deepak)
echo "First Element: ${VAR[0]}"
echo "First Element: ${VAR[1]}"
echo "First Element: ${VAR[2]}"
echo "First Element: ${VAR[3]}"

The output of this script:

Beginners Tutorial to use Bash For Loop
Bash for loop array

Let us take a practical example to understand loop with array in real time environment.
Assume you have to create 5 users and assign a password to these users. In this shell script I will assign "Passw0rd" as password to all the users

#!/bin/bash

USERS=(user1 user2 user3 user4 user5)

for user in ${USERS[@]}
do
    echo "Creating user $user"
    useradd $user
    echo "Assigning password for $user"
    echo "Passw0rd" | passwd "$user" --stdin
done

 

EX_4: Using for loops in bash as C programmers

  • Bash extends the for keyword to provide functionality similar to the three-argument for loop used in C using double parenthesis
  • We can use the above examples when we have a small number of range to iterate over but what if we have a larger range of items?
  • Or if we do not know the number of times the loop has to run and instead we get this value internally from the script?

Syntax:

for ((ASSIGN_VALUE; ASSIGN_LIMIT ; STEP_UP)) 
do
  [COMMANDS]
done
  • The syntax would be more clear in this sample script.
  • The first expression is run before the loop starts: We assign the i value to zero to start it off.
  • The second expression is the test used to determine whether the loop should continue or stop: We test whether the value of i is less than 5.
  • The third expression runs after each instance of the loop: We add one to the value of i.
#!/bin/bash

for (( i=0; i<=5; i++ ))
do
    echo "Iteration $1"
done

The output from this script:

Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

Now you must be wondering do we have any use case for such of "for loop" in real time scenario.

The answer would be YES, very much. I will share some real time scenarios to help you understand better:

  • In this example I will search for all files with the syntax matching file* under /tmp
  • Then append some content to these files
  • Now the loop should iterate based on the number of files found
  • Since the iteration starts from 0 we have given variable i to be one less than the number of files which is found
#!/bin/bash

file_path=(`find /tmp -name "file*" -type f`)

echo "Found ${#file_path[@]} files"
for (( i=1; i<${#file_path[@]}; i++ )) 
do 
     echo "Adding content to ${file_path[$i]}" 
     echo "add some content" >> "${file_path[$i]}"
done

The output from this script:

Beginners Tutorial to use Bash For Loop
bash for loop example

 

Define multiple variables

We can also use multiple variables in single for loop. In this example I have defined two variables where the loop will iterate until i is less than equal to  j variable

#!/bin/bash

for (( i=1,j=5; i<=j; i++,j-- ))
do
    echo "Iteration for i=$i"
    echo "Iteration for j=$j"
done

The output from this script

Iteration for i=1
Iteration for j=5
Iteration for i=2
Iteration for j=4
Iteration for i=3
Iteration for j=3

In this script I will perform some more operations using two variables in single for loop with bash

for (( i=0, j=0 ; i+j < 5 ; i++, j++ ))
do
    echo "Value for i=$i"
    echo "Value for j=$j"
    echo "Multiply: $((i*j))"
    echo "add: $((i+j))"
done

The output from this script

Value for i=0
Value for j=0
Multiply: 0
add: 0
Value for i=1
Value for j=1
Multiply: 1
add: 2
Value for i=2
Value for j=2
Multiply: 4
add: 4

 

EX_5: Use continue statement

Now it is not mandatory that you would want to perform task for complete iteration, it is possible you may have a specific requirement to ignore certain value during the iteration run
I have this sample script where the loop iterates for 5 times

for (( i=0 ; i<=5 ; i++ ))
do
    if [ $i -eq 2 ];then
       echo "Don't do anything when i=2"
    fi
    echo "Doing something when i=$i"
done

Now I have an additional check when value of the variable i is equal to 2, so when this matches the script should do nothing and continue with the remaining iteration

But let us check the output from this script

Doing something when i=0
Doing something when i=1
Don't do anything when i=2
Doing something when i=2
Doing something when i=3
Doing something when i=4
Doing something when i=5

Even when I added a check to ignore i=2 match, still we see "Doing something when i=2", so how to fix this? We need to use continue statement for such case

The continue statement will exit the current iteration of the loop and will continue with the next iteration in the loop

for (( i=0 ; i<=5 ; i++ ))
do
    if [ $i -eq 2 ];then
       echo "Don't do anything when i=2"
       continue
    fi
    echo "Doing something when i=$i"
done

The output from this script

Doing something when i=0
Doing something when i=1
Don't do anything when i=2
Doing something when i=3
Doing something when i=4
Doing something when i=5

Now we see that when variable of i is equal to 2, the iteration is skipped and the loop continues with remaining iteration

 

EX_6: Use break statement

With continue we were able to skip the loop for single iteration but what if we want to completely come out of the loop with a certain condition?
In this script we want to end the loop if the value of i variable is equal to 2 so with the if condition I have added a break statement

for (( i=0 ; i<=5 ; i++ ))
do
    if [ $i -eq 2 ];then
       echo "Exiting, match sucessful"
       break
    fi
    echo "Doing something when i=$i"
done

The output from this script

Doing something when i=0
Doing something when i=1
Exiting, match successful

As expected, our for loop terminates when the if condition is fulfilled using the break statement.

 

EX_7: One liner for loop examples

We can write for loop in one liner commands to perform simple tasks using below syntax:

for {ELEMENT} in ${ARRAY[@]}; do [COMMAND_1]; [COMMAND_2]; [COMMAND_3]; done

In this shell script I will list all the network devices available on my server. here instead of defining an ARRAY, I have provided a PATH with *, so all the files and directories under that PATH would be considered as ELEMENT of the ARRAY

# for int in `ls /proc/sys/net/ipv4/conf/`;do echo "Interface: $int"; done
Interface: all
Interface: bond0
Interface: bond1
Interface: default
Interface: eth0
Interface: eth1
Interface: eth2
Interface: eth3
Interface: lo

In this script I will remove all files under /tmp matching regex file*

# for file in /tmp/file*; do rm -vf $file; done
removed '/tmp/file1'
removed '/tmp/file2'
removed '/tmp/file3'

 

Conclusion

Even though bash is loosing it's popularity with new programming language coming every day but engineers and developers still prefer bash to automate day to day tasks which consists of system commands. In such case it is always handy for engineers to be aware of using such loop based iteration.

I have given some very basic examples for beginners to understand the syntax but this can be used to do complex tasks as well. Lastly I hope this tutorial to learn bash for loop with examples to iterate over a range and series of items on Linux and Unix was helpful. So, let me know your suggestions and feedback using the comment section.

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment