Signup/Sign In

Bash For Loop

For loop in Bash Script is used to execute specified statements/commands repeatedly. Bash For loop is similar to the loop in C language. If you are familiar with the C language then it is easy to understand the concept but if not then don't worry it is very simple to understand.

Think loop as a body of some code statements that execute till the specified condition. In Bash, a loop can also be used to iterate till the item is present in the list, array, string, etc. The general syntax of for loop is given below.

Bash For Loop Syntax

for item in [LIST]
do
  [COMMANDS]
done

Let's learn by creating some examples.

Bash For Loop To Iterate String Elements

We can use for loop to traverse string elements. It treats each word as an element and iterates till the elements are present. It does not require any condition because it automatically stops when the items are finished.

VAL="India is my country"
for word in $VAL
do
  echo "word: $word"
done


word: India
word: is
word: my
word: country

Bash For Loop To Iterate Over a Range

We can use for loop to transverse values of a range. In Bash, the range can be created by using the curly braces, so we passed the range in the loop, and the loop iterate till the max value of the range.

Here, the range includes 0 to 5 values.

for i in {0..5}
do
  echo "i= $i"
done


i= 0
i= 1
i= 2
i= 3
i= 4
i= 5

Bash For Loop To Print Even Values

In the range, the first value represents a starting point, the second represents the endpoint, and the third represents the skip point for each iteration. Here, the loop will skip every second iteration and will execute only for the even values. See the example below.

for i in {0..10..2}
do
  echo "i= $i"
done


i= 0
i= 2
i= 4
i= 6
i= 8
i= 10

Bash For Loop To Iterate Over an Array

For loop can also be used to traverse the array elements. This is the most common use of loop when we want to print elements of an array and use for loop for the same.

INTEGER=(1 2 3 4)
for i in "${INTEGER[@]}"; do
  echo "i: $i"
done


i: 1
i: 2
i: 3
i: 4

C Style For Loop In Bash

We can use for loop in the same manner as the C language. This loop contains three parts: initialization, condition, and increment.

In the first part, a variable is initialed with an initial/start value, then in condition, the end value is specified which means the loop can go only till the specified end value. and in the last, the increment value is specified that basically increment the initialized variable by one.

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


i= 0
i= 1
i= 2
i= 3
i= 4
i= 5

Break Statement in Bash For Loop

The break statement is used to stop the current code execution flow and the control goes outside the scope.

In the for loop, we can use the break statement to stop its execution and get control back to the for loop. You can see the loop prints only the first 3 values and when it reached 3 then due to the break statement it stops execution.

for ((i = 0 ; i <= 5 ; i++)); do
   if [[ "$i" == 3 ]]; then
      break
   fi
   echo "i= $i"
done


i= 0
i= 1
i= 2

Continue Statement in Bash For Loop

The continue statement is used to skip the current code execution flow and the control goes to the next iteration of the loop.

In the for loop, we can use the continue statement to skip its execution. You can see the loop prints all the values except 3 because when it reaches 3 then due to the continue statement it skips execution and resumes with the next iteration that is 4.

for ((i = 0 ; i <= 5 ; i++)); do
   if [[ "$i" == 3 ]]; then
      continue
   fi
   echo "i= $i"
done


i= 0
i= 1
i= 2
i= 4
i= 5

Conclusion

In Bash, for loop is commonly used to transverse the elements of a list, array, string, range, etc. We can also use it to iterate/repeat the execution till the specified condition. The main purpose of the loop is basically to execute tasks repeatedly. So, we can use it to repeat any statement/command in Linux.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.