×
How to Use Bash For Loop in Linux

Loops are a set of commands that allow us to take a series of commands and keep re-running them until a particular situation is reached. In Bash scripting, there are 3 basic loop constructs for, while and do-while/repeat-until loop.

You can apply for loop on bash script in various ways. The for loop is different from other programming languages. Basically, it let’s you iterate over a series of ‘words’ within a string.

In this tutorial, we will explain how to use for loops in a Bash script with example.

Syntax of For Loop

The basic syntax of for loop is shown below:


for item in [LIST]
do
[COMMANDS]
done

Where LIST is a series of strings, numbers and an array. And COMMANDS is a series of commands.

Reading String Value

Let’s create a shell script named test.sh.

nano test.sh

Add the following lines:


#!/bin/bash
for i in a b c d e f
do
echo "String : $i"
done

Save the file. Then, run this script as shown below:

bash test.sh

Output:

String : a
String : b
String : c
String : d
String : e
String : f

In the above example the loop will iterate from a to f.

You can also use the sequence expression to specify a range of characters by defining a start and the end point of the range.

Now, edit the test.sh file.

nano test.sh

Make the following changes:


#!/bin/bash
for i in {a..h}
do
echo "String : $i"
done

Save the file and run it again.

bash test.sh

You should see the following output:


String : a
String : b
String : c
String : d
String : e
String : f
String : g
String : h

Reading Array Value

You can use for loop to iterate the values of an array.

To do so, create a new shell script named test1.sh:

nano test1.sh

Add the following lines:


#!/bin/bash
MonthList=("Jan Feb March April May June")
for month in $MonthList
do
echo "Month : $month"
done

Save the file then run the script with the following command:

bash test1.sh

You should see the following output:


Month : Jan
Month : Feb
Month : March
Month : April
Month : May
Month : June

Reading Command-line Arguments

You can also use Command-line arguments values with bash for loop.

For example, let’s create a new bash script name test2.sh:

nano test2.sh

Add the following lines:


#!/bin/bash
for val in $*
do
echo "My Argument: $val"
done

Save and close the file.

Then, run the above bash script with argument “I am Hitesh” as shown below:


bash test2.sh I am Hitesh

You should see the following output:


My Argument: I
My Argument: am
My Argument: Hitesh

Reading Content of File

You can read the content of the file using bash for loops.

For example, create a file named month.txt as shown below:

nano name.txt

Add the following content:


Hitesh
Jayesh
Vyom
Ninav
Disha

Save and close the file. Then, create a shell script named test3.sh:

nano test3.sh

Add the following lines:


#!/bin/bash
i=1
for var in `cat name.txt`
do
echo "NameList $i: $var"
((i++))
done

Save and close the file. Then, run the script with the following command:

bash test3.sh

You should see the following output:


NameList 1: Hitesh
NameList 2: Jayesh
NameList 3: Vyom
NameList 4: Ninav
NameList 5: Disha

Infinite For Loops

You can use empty expressions to create infinite loops.

For example, create a test4.sh file:

nano test4.sh

Add the following lines:


#!/bin/bash
for (( ; ; ))
do
echo "This is Infinite Loops"
done

Save the file. Then, run the script as shown below:

bash test4.sh

Output:


This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops
This is Infinite Loops

You can hit CTRL+C to stop the infinite loops.