Every programming and scripting language has a few looping statements up its sleeve to save programmers some time when trying to do repetitive tasks.
In this article, we’re going over the while loop command in Bash to help you write better scripts.
What is the while loop?
The while loop is an entry controlled loop statement that will keep repeating a given set of instructions until a specified condition is met.
Entry controller here means that the loop checks the condition before going over the set of commands., which means that if the given condition is false the first time around, the loop won’t run at all.
Also read: Bash ‘if else’ statement examples
Bash while loop basics
The basic syntax of the while loop is as follows.
while [loop condiiton] do task 1 task 2 task 3 done
The loop will keep on repeating tasks one to three for as long as the loop condition is true in the above snippet. Once the condition returns false, the loop will break.
You can even make one line loops.
while [loop condition]; do Tasks; done
Programmers can also make infinite loops by leaving out the loop condition when writing the while command. Alternatively, you can mention a condition or any other statement like true that would never return false, making the loop infinite.
while : do echo "Press <CTRL+C> to exit." sleep 1 done
The snippet above will keep printing Press <CTRL+C> to exit infinitely until the user terminates the program using the keyboard interrupt (Ctrl+C).
Here’s the single line equivalent of the aforementioned snippet.
while :; do echo 'Press <CTRL+C> to exit.'; sleep 1; done
Also read: How to compare Strings in Bash?
break and continue statements
The break and continue statements are crucial to controlling the flow of just about any loop.
The break statement is used to terminate the loop and pass the program control to the next command in the line. The statement is usually used to terminate the loop early based on a certain condition.
i=0 while [ $i -lt 5 ] do echo "Number: $i" ((i++)) if [[ "$i" == '4' ]]; then break fi done echo 'Loop terminated
The above snippet will terminate the loop as soon as the value of the item being iterated reaches four.
The continue statement
This statement allows you to skip the current iteration and move on to the next one, essentially skipping an iteration midway. The statement is often used with conditional statements as well.
i=0 while [ $i -lt 5 ] do ((i++)) if [[ "$i" == '2' ]]; then continue fi echo "Number: $i" done echo 'Loop terminated'
The above snippet will run normally as long as the value of the iterated item reaches two. Once that condition is met, the continue statement kicks in and skips that particular iteration.
Also read: How to exit Bash script?
Using while to read a file line by line
One of the most common use cases for a while loop is to read data from files one line at a time. In the example below, we’re reading the /etc/text file and printing each line.
file=/etc/text while read -r line; do echo $line done < "$file"
Instead of controlling the loop with a condition, we use input redirection to pass a file to the read command, which controls the loop. This makes the loop run until we reach the last line of the file.
Note the use of the -r flag to prevent backlash from being interpreted as an escape character.
By default, the read command removes whitespace characters following and leading each word. You can overwrite this behaviour by using the IFS command before the read command.
while IFS= read -r line; do echo $line
Also read: How to use SCP command to transfer files in Linux?