Bash While Loop Tutorial

Linux bash is like a programming or scripting language that provides similar structures to the programming languages like C#, PHP, Python, C, etc. The while loop also provided by the Linux bash which is simply used to iterate over multiple items which can be numbers, files, folders, users, etc.

Bash While Loop Syntax

The bash while loop is very flexible where it provides multiple syntaxes to be used. The original and most popular syntax is like below.

while [CONDITION]
do
   CODE
done
  • [CONDITION] is the condition that is checked in every iteration of the while loop. If the CONDITION is true the loop continues if not true the loop ended.
  • CODE is a single or multiple lines of code that is executed in every iteration.
  • do is a statement used to start the while loop code block.
  • done is a statement used to end the while loop code block.

Alternatively yhe while loop can be expressed as a single line like below.

while [CONDITION]; do CODE; done

While Loop For Numbers

We will start with a simple example where we will loop over numbers. We will create a variable named x which is set to 1 and we will iterate until the x reached 10. In every iteration, the x will be increased by one and printed to the screen.

x=1

while [ $x -le 5 ]
do
  echo "Current value of x is $x"
  x=$(( $x + 1 ))
done

This while loop example output will be like below.

Current value of x is 1
Current value of x is 2
Current value of x is 3
Current value of x is 4
Current value of x is 5

This while loop example can be also express as a one liner while loop like below.

x=1 ; while [ $x -le 5 ] ; do echo "Current value of x is $x" ; x=$(( $x + 1 )); done

While Loop For Files

The while loop can be used to loop over the files. We will use the ls command with the -p option which will list directories with slash (/) sign. By using the grep we will remove lines containing slash which are directories. The output is redirected into the while loop and read into the variable $file.

ls -p | grep -v /  | while read file 
do 
   echo ${file}
done

The output is like below.

a.out
 file1
 file2
 file3
 file4
 myfile
 mylink
 names.txt
 newfile
 nmap-7.91.tar.bz2
 random.c
 read_line_by_line.sh
 sample.txt
 test
 text.txt
 userinput.py
 while.sh

While Loop For Directories

Looping with while on the directories are similar to the files. We will just filter files by using the grep command with lines those contains slash.

ls -p | grep /  | while read directory 
do 
   echo ${directory}
done

The looped directories are like below.

data1/
 data2/
 data3/
 data4/
 data5/
 Desktop/
 Documents/
 Downloads/
 Music/
 myfolder/
 Pictures/
 Public/
 Templates/
 thinclient_drives/
 Videos/
 year/

While Loop For File Lines

Another use case for the bash while loop is iterating or looping over the lines of a file. The file read with the read command and every line is iterated with the $line variable. We will redirect the file sample.txt into the while loop and read each line into the $line variable.

while read line 
do
  echo "$line"
done < sample.txt

Break While Loop

The bash while loop iterates continuously without breaking. But in some cases, we may want to stop and exit from the while loop if some specified condition is met. The break keyword is used to break a while loop and exit. In the following example, we will break the while loop if the $x is equal to the 4.

x=1

while [ $x -le 5 ]
do
  if [ $x -eq 4 ]
  then
    break
  fi
  echo "Current value of x is $x"
  x=$(( $x + 1 ))
done

The output is like below.

Current value of x is 1
Current value of x is 2
Current value of x is 3

Continue/Skip Next Step In While Loop

During the iteration with the while loop in bash, some steps can be skipped. The continue statement can be used to skip some steps according to the provided condition which is generally set with the if statement. In the following example, we will skip if the $x is 3 and continue with the next step or iteration.

x=1

while [ $x -le 5 ]
do
  if [ $x -eq 3 ]
  then
    x=$(( $x + 1 ))
    continue
  fi

  echo "Current value of x is $x"

  x=$(( $x + 1 ))
done

The output is like below.

Current value of x is 1
Current value of x is 2
Current value of x is 4
Current value of x is 5

Infinite While Loop

Generally, the while loop is iterated for a specific number of times. But we may need to iterate continuously unless a specific condition is met or the processes are stopped. The infinite loop can be created with different conditions where every condition should return true every time.

while true 
do
  echo "I am going to infinite"
done

Some part of the output is like below where every line is the same.

...
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
I am going to infinite
...

Nested While Loop

Another while loop can be used inside a while loop in bash. This is called as nested while loop as they are nested together.

x=0

while [ $x -le 9 ]
do
  y=0

  while [ $y -le 9 ]
  do
    echo "$x$y"
    y=$(( $y + 1 ))
  done

  x=$(( $x + 1 ))
done

The output of the nested while loop is like below.

00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
...
99

Execute While Loop As Bash Script

Bash is called a scripting language where bash scripts can be put into a file and executed like a script, application, or a command. First, put the while loop into the bash file named while.sh . We will add the “#!/bin/bash” line in order to set the scripting interpreter as bash.

#!/bin/bash

x=1

while [ $x -le 5 ]
do
  echo "Current value of x is $x"
  x=$(( $x + 1 ))
done

Then we will make this while.sh file directly executeable with the following chmod command.

chmod u+x while.sh

Now we can run the while loop as a bash script like below directly calling.

./while.sh

Leave a Comment