Loops are one of the fundamental concepts of programming languages. Loops are handy when you want to run a series of commands number of times until a particular condition is met.
In scripting languages such as Bash, loops are useful for automating repetitive tasks. There are 3 basic loop constructs in Bash scripting, for loop, while loop, and until loop.
This tutorial explains the basics of while loops in Bash as well as the break and continue statements to alter the flow of a loop.
Bash while Loop
The while loop is used to performs a given set of commands an unknown number of times as long as the given condition evaluates to true.
The Bash while loop takes the following form:
while [CONDITION]
do
[COMMANDS]
done
The condition is evaluated before executing the commands. If the condition evaluates to true, commands are executed. Otherwise if the condition evaluates to false the loop will be terminated and the program control will be passed to the command that follows.
In the example below, on each iteration the loop prints the current value of the variable i
and increments the variable by one.
i=0
while [ $i -le 2 ]
do
echo Number: $i
((i ))
done
Tue loop iterates as long as i
is less or equal than two. It will produce the following output:
Number: 0
Number: 1
Number: 2
Infinite while Loop
An infinite loop is a loop that repeats indefinitely and never terminates. If the condition always evaluates to true you get an infinite loop.
In the following example we are using the built-in command :
which always returns true to create an infinite loop. You can also use the true
built-in command or any other statement that always returns true.
while :
do
echo "Press to exit."
sleep 1
done
The while loop above will run indefinitely. You can terminate the loop by pressing CTRL C
.
Here is a single-line equivalent:
while :; do echo 'Press to exit.' ; sleep 1; done
Read a File Line By Line
One of the most common usages of the while loop is to read a file, data stream or variable line by line.
In the following example the while loop will read the /etc/passwd
file line by line and print each line.
file=/etc/passwd
while read -r line; do
echo $line
done < "$file"
Instead of controlling the while loop with a condition we are using input redirection (< "$file"
) to pass a file to the read
which is the command that controls the loop. The while loop will run until the last line is read.
When reading file line by line always use read
with the -r
option to prevent backslash to act as an escape character.
By default the read
command trims the leading/trailing whitespace characters (spaces and tabs). Use the IFS=
option before the read
command to prevent this behavior
file=/etc/passwd
while IFS= read -r line; do
echo $line
done < "$file"
Break and Continue Statements
The break and continue statements can be used to control the while loop execution.
Break Statement
The break statement terminates the current loop and passes program control to the command that follows the terminated loop. It is usually used to terminate the loop when a certain condition is met.
In the following example, the execution of the loop will be interrupted once the current iterated item is equal to 2
.
i=0
while [ $i -lt 5 ]
do
echo "Number: $i"
((i ))
if [[ "$i" == '2' ]]; then
break
fi
done
echo 'All Done!'
Number: 0
Number: 1
All Done!
Continue Statement
The continue statement exits the current iteration of a loop and passes program control to the next iteration of the loop.
In the following below, once the current iterated item is equal to 2
the continue statement will cause execution to return to the beginning of the loop and to continue with the next iteration.
i=0
while [ $i -lt 5 ]
do
((i ))
if [[ "$i" == '2' ]]; then
continue
fi
echo "Number: $i"
done
echo 'All Done!'
Number: 1
Number: 3
Number: 4
Number: 5
All Done!
Conclusion
By now you should have a good understanding of how to use the bash while loop.
If you have any questions or feedback, feel free to leave a comment.