Skip to content

Bash While loop explained

  • by
  • 3 min read

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]
   task 1
   task 2
   task 3

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 :
  echo "Press <CTRL+C> to exit."
  sleep 1

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. 

break statement

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. 


while [ $i -lt 5 ]
  echo "Number: $i"
  if [[ "$i" == '4' ]]; then

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.


while [ $i -lt 5 ]
  if [[ "$i" == '2' ]]; then
  echo "Number: $i"

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.


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?

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here:

Exit mobile version