Generally speaking, the while loop is used to execute one or more commands (statements) until the given condition is True. As the condition becomes false, the execution moves to the next line of code outside of the while loop.
The examples can be reading line by line in a file or stream until the file ends.
Syntax for using the while loop
The general syntax for using the Bash while loop is:
while [condition] do //code to execute here //increment/decrement done
So, this is how the while loop in Bash works:
- After the while keyword, the condition is given in the brackets.
- If the condition evaluates as True, the code after the do keyword executes.
- In the end, generally, the increment/decrement of the variable is given.
- The execution moves back to condition and keeps on executing the above process until the condition becomes false.
- As the condition becomes False, the execution moves outside of the while loop; after the done
A simple example of using the while loop
In the first example for explaining how while loop works in Bash, we have a variable which value increments in each iteration. Its value is tested in the condition part and as long as the condition is True, its value is displayed:
x=1 while [ $x -le 10 ] do echo "$x" ((x++)) done
The result:
1
2
3
4
5
6
7
8
9
10
The example with the decrement
Now see an example where the value of the variable is decremented in each iteration:
x=10 while [ $x -ge 1 ] do echo "$x" ((x--)) done
The output:
10
9
8
7
6
5
4
3
2
1
Reading a file line by line using while loop
As mentioned earlier, one of the uses of the while loop can be reading the text file or streams by using the while loop.
In the example below, we have a text file placed in the “D” directory. We will read the contents of the text file line by line by using the while loop and display its content as follows:
while read fileCont do echo $fileCont done < D:/test/bash-tst.txt
The output:
hellotesting123
What if a condition is False at first attempt?
So what this while with “do” keyword behaves if the condition is False upfront? For that, consider the first example in this tutorial where I made a little change and assigned the value 11 to variable x initially.
So, the condition is False and see the output yourself.
The code:
x=11 while [ $x -le 10 ] do echo "$x" ((x++)) done echo "Execution is out of while loop"
The output:
You can see, not a single time the value of the variable is displayed. As the condition is false in the first attempt so, execution got out of the while loop.
So, how to implement do..while in Bash?
As such, the do..while loop is a type that executes the code inside the loop at least once, even the condition is false at first attempt. How we can implement this in Bash?
First, have a look at this example and output and I will explain how it worked:
x=11 while echo "$x" [ $x -le 10 ] do ((x++)) :; done
The output:
You can see, the condition is false yet it displayed the value of the variable. This is because the condition is not required to be tested immediately after the while keyword.
Due to this flexibility, you may achieve the do..while purpose easily by placing the condition after the statements to be executed in the loop.
The example of break statement with while loop
The break statement is used to omit the loop and moving the control to the next line where that break statement is used.
Generally, this is helpful in scenarios where a task is accomplished in a while or another loop and you want to exit at that stage.
The example below shows using the break statement. We will exit the while loop as the value of the variable is equal to 50 (increments by 10 in each iteration):
x=10 while [ $x -le 100 ] do echo "$x" ((x=x+10)) if [[ "$x" == '50' ]]; then break fi done
The output:
10
20
30
40
Normally, it should keep on iterating the while loop until the value of the variable is 100 or less. However, the break statement made the loop exiting at value 50.
How to use continue statement with the while loop
The continue statement just omits the current iteration rather than exiting the loop completely. The example below shows how:
x=0 while [ $x -le 100 ] do ((x=x+10)) if [[ "$x" == 50 ]]; then continue fi echo "$x" done
The result:
10
20
30
40
60
70
80
90
100
110
You see, we checked for the variable value 50. As it reached 50, the if statement became true and continue statement executed. That is why the value 50 is not displayed.