Module-6 How to use While and Until loop in Bash scripts

Table of contents

Introduction

In this article we will be covering in detail on how to use the while and until loop on your bash scripts with practical examples.

While loop is used to execute set of command for ‘n’ number of times as long as the condition is true and the loop exits when the condition is false 

Until loop  is used to execute a set of commands for ‘n’ number of times as long as the condition is false and the loop exits when the condition is true. We can say it is a reverse of what while loop does 

You might be interested in understanding different conditionals we can use with the While and until loop. Our article on Bash conditionals will explain the different conditionals we can consider to use with while loop and until loop. Please click the below link to navigate to that article.

Module-4 How to use conditionals in bash scripting

Syntax for While and Until loop

Following syntax can be used for while loop in Bash

while [condition should be true ]
do
   # (command statements)
done

Following syntax can be used for until  loop in Bash

until [condition should be false ]
do
   # (command statements)
done 

How to use integer conditionals with while and until loop 

Integer Conditionals with While loop

In this example , we are creating a variable “number=1” then we are entering a while loop as long as we satisfy the condition “number should be less than 3 “. When this condition is false we exit the while loop. But when the condition is true we are printing it on the screen and also incrementing the number by 1  for the next iteration

#script 
[root@discoveringsystems while-until-loops]# cat basic-while-loop
#!/bin/bash

number=1
while [ $number -lt 3 ]
do

  echo "Number is $number"
  ((number++))

done
#script execution
[root@discoveringsystems while-until-loops]# ./basic-while-loop
Number is 1
Number is 2

Integer conditionals with until loop

In this example , we are creating the integer variable “number=1” and looping through the value with increments until we hit the conditional to be false. If the condition is true then the until loop exits the loop

#script
[root@discoveringsystems while-until-loops]# cat basic-until-loop
#!/bin/bash

number=1
until [ $number -gt 3 ]
do

  echo "Number is $number"
  ((number++))

done
#script execution
[root@discoveringsystems while-until-loops]# ./basic-until-loop
Number is 1
Number is 2
Number is 3

How to use while loop to read lines from a file and iterate through it

In this example we will see how to use read command along with the while loop to iterate through the lines in a text file provided as input 

Following are the contents of the book.txt file , please note that there are some empty lines in between the lines 

[root@discoveringsystems while-until-loops]# cat book.txt
this is the first line

this is the second line



this is the third line

The following script takes book.txt as input or the while loop , use the read command to read the contents line by line. While reading a line , it stores the line as a variable Parsed_text.  Also I have added to iteration counter to check how many iterations were made while parsing through the book.txt  , while reading through the empty lines it is counted as an iteration

#script
[root@discoveringsystems while-until-loops]# cat basic-while-loop
#!/bin/bash

iteration=0
while read Parsed_text
do

  echo "$Parsed_text"
  echo "Iteration $iteration"
  ((iteration++))

done < book.txt

While the script is executed we can see that  each line was echoed along with their iteration count 

#script execution
[root@discoveringsystems while-until-loops]# ./basic-while-loop
this is the first line
Iteration 0

Iteration 1
this is the second line
Iteration 2

Iteration 3

Iteration 4

Iteration 5
this is the third line
Iteration 6

The same script can be also modified in a way that it takes the piped inputs from other commands

In this example , the same script has been modified and this we are using the Cat command to read the same book.txt and piped it to provide that as input to the while loop and then the read command reads line by line and while loop iterates through it

#script
[root@discoveringsystems while-until-loops]# cat basic-while-loop
#!/bin/bash

iteration=0

cat book.txt | while read Parsed_text
do

  echo "$Parsed_text"
  echo "Iteration $iteration"
  ((iteration++))

done
#script execution
[root@discoveringsystems while-until-loops]# ./basic-while-loop
this is the first line
Iteration 0

Iteration 1
this is the second line
Iteration 2

Iteration 3

Iteration 4

Iteration 5
this is the third line
Iteration 6

How to use continue and the Break statements with while and until loop

Break statement is used to terminate a loop when that statement is hit due to matching any conditionals we incorporate in the same loop iteration 

Continue statement is used to skip the remaining statements part of that loop iteration and move on to the next iteration. To use the continue statement we can use the conditionals part of the loop iteration and we can hit it if the condition is true to the iteration variable or item

How to use break statement with while loop

In the below example ,we were initializing the count to be 0 and it enters the While loop. As long as the condition is true we do the iteration. Then we used the break statement in the while loop after we matched the condition. This takes us out of the while loop.

#script
[root@discoveringsystems while-until-loops]# cat basic-while-loop
#!/bin/bash

count=0

while [ $count -lt 5 ]
do

  echo "$count"
  if (($count==1)); then
    echo "we broke the loop using break statement"
    break
  fi
  ((count++))

done

#script execution
[root@discoveringsystems while-until-loops]# ./basic-while-loop
0
1
we broke the loop using break statement

How to use break statement with until loop

In the below example ,we were initializing the count to be 0 and it enters the until  loop. As long as the condition is False we do the iteration. Then  we used the break statement in the until loop after we matched the condition. This takes us out of the until  loop

#script
[root@discoveringsystems while-until-loops]# cat basic-until-loop
#!/bin/bash

count=0

until [ $count -gt 5 ]
do

  echo "$count"
  if (($count==2)); then
    echo "we broke the loop using break statement"
    break
  fi
  ((count++))

done
#script execution
[root@discoveringsystems while-until-loops]# ./basic-until-loop
0
1
2
we broke the loop using break statement

How to use continue statement with while loop

In the below example ,we were initializing the count to be 0 and it enters the While  loop. As long as the condition is true we do the iteration. Then  we used the continue statement in the while loop after we matched the condition. This skips the further steps part of that iteration and takes us to the next iteration 

#script 
[root@discoveringsystems while-until-loops]# cat basic-while-loop
#!/bin/bash

count=0

while [ $count -lt 5 ]
do

  ((count++))
  if (($count==3)); then
    echo "we skipped the iteration using continue statement"
    continue
  fi
  echo "$count"


done
#script execution
[root@discoveringsystems while-until-loops]# ./basic-while-loop
1
2
we skipped the iteration using continue statement
4
5

How to use continue statement with until loop

In the below example ,we were initializing the count to be 0 and it enters the until  loop. As long as the condition is false we do the iteration. Then  we used the continue statement in the until loop after we matched the condition. This skips the further steps part of that iteration and takes us to the next iteration

#script
[root@discoveringsystems while-until-loops]# cat basic-until-loop
#!/bin/bash

count=0

until [ $count -gt 5 ]
do

  ((count++))
  if (($count==3)); then
    echo "we skipped the iteration using continue statement"
    continue
  fi
  echo "$count"


done
#script execution
[root@discoveringsystems while-until-loops]# ./basic-until-loop
1
2
we skipped the iteration using continue statement
4
5
6

Special options with the break and the continue statement

Both the break statement and the continue statement has features to help exit multiple nested while , for or until loops  ,the syntax is as follows 

continue [number of nested loops to skip ]
#Eg : continue 2

break [number of nested loops to break ]

#Eg: break 2

Lets see one practical example of using break 2 in a script. When we specify with 2 or more it will break out of the nested loop 

If the bash script has nested while loop like the following , we can see that i made sure the condition matches for the inner while loop and made it to execute the break 2 , so it broke out of both the while loop and the script ended there 

#script 
[root@discoveringsystems while-until-loops]# cat basic-while-loop
#!/bin/bash

count=0

while [ $count -lt 5 ]
do

  echo "$count"
  ((count++))
  count_2=10
  while [ $count_2 -lt 15 ]
  do
    echo "$count_2"
    ((count_2++))
    if (($count_2==13)); then
      echo "we broke the iteration using break statement"
      break 2
    fi
  done

done

#script execution 
[root@discoveringsystems while-until-loops]# ./basic-while-loop
0
10
11
12
we broke the iteration using break statement

Leave a Comment

Your email address will not be published. Required fields are marked *