For Loop In Bash Script

Linux Bash Shell provides the For Loop in order to iterate over the given array, list, string, etc. For loops are very useful for iterative operations and heavily used in Linux bash shells. In this tutorial, we examine different ways and examples to use for loops in the Bash shell.

for Loop Syntax

The bash for loop has 3 different syntaxes. The 3 syntaxes of the for loop are like below.

for VARIABLE in 1 2 3 4 5 .. N
do
   FOR_LOOP_BODY
done
  • VARIABLE is the current value.
  • FOR_LOOP_BODY is a single or more lines that is executed every iteration.
for VARIABLE in FILE1 FILE2 ... FILEN
do

   FOR_LOOP_BODY
done
  • VARIABLE is the current value.
  • FOR_LOOP_BODY is a single or more lines that is executed every iteration.
for OUTPUT in $(COMMAND)
do

   FOR_LOOP_BODY
done
  • OUTPUT is the $(COMMAND) output which can be used inside the FOR_LOOP_BODY
  • $(COMMAND) is executed one time and its output is iterated multiple times.
  • FOR_LOOP_BODY is executed in every iteration of the for loop.

For Loop In Number List

A number list can be used with the for loop. The numbers are provided to the for loop by separating them with spaces. The current number is set $i in every iteration. The current variable is printed in the for loop value.

#!/bin/bash

for i in 1 2 3 4 5 6 7 8 9
do
   echo "Current variable is $i"
done
Current variable is 1
Current variable is 2
Current variable is 3
Current variable is 4
Current variable is 5
Current variable is 6
Current variable is 7
Current variable is 8
Current variable is 9

For Loop with Number Range

The number range can be used with a bash script for loop. In the following example, we define a number range with { .. } syntax. The start number is 1 and the end number is 9 in this example.

#!/bin/bash

for i in {1..9}
do
   echo "Current variable is $i"
done

For Loop with Start, End, Increment

Another bash script for loop syntax and usage is to specify the range with increment value. In the following bash script, we create a for loop which uses the number list with increment value 2.

#!/bin/bash

for i in {0..20..2}
do 
   echo "Current variable is $i"
done

For Loop with Seq (Sequence) Command

Linux bash provides the seq command which can be created number sequences easily. In the following example, we start a sequence from 0 and increment it 2 by 2 until reaching 20. The sequence is iterated with the bash for loop like below.

#!/bin/bash

for i in $(seq 0 2 20)
do
   echo "Current variable is $i"
done

Infinite For Loop

Bash infinite for loop can be easily created by setting 3 parameters empty like below. This bash for loop is treated unless the process exit with a kill signal or processes interrupts.

#!/bin/bash

for (( ; ; ))
do
   echo "Guess my age:"
   read myage
done

Exit For Loop

The exit statement can be used to exit from a bahs for loop. Just run the exit statement where you want to exit from the bash for loop.

#!/bin/bash

for (( ; ; ))
do
   echo "Guess my age:"
   read myage
   exit
done

For Loop with Array Elements

The bash for loop can be used to iterate over array elements. In the following example, we create a bash array names and iterate over every element by using a bash script for loop like below.

#!/bin/bash

NAMES=('ismail' 'ahmet' 'ali' 'elif')
 
for name in "${NAMES[@]}"
do

   echo $name
done
ismail
ahmet
ali
elif
See also  How To Make Bash Script Executable with chmod?

Leave a Comment