This article will show you how to use while loops in Bash/Shell scripts, and provides some code examples.
Bash scripts let you automate tasks in the linux shell. Often, you’ll want to repeat a task for a set of data or repeated user input – that’s what while loops are for – they let you loop or iterate over a sequence of data or input, making it easy to build scripts that repeat a set of actions.
The while loop syntax demonstrated below will also work for the Zsh shell and other popular Unix shells.
Bash while Loop Syntax
The syntax for while loops in Bash is as follows:
while [ CONDITION ] do # Commands to execute for each iteration go here done
- Any number of commands can be executed within the loop
- the loop will continue to execute for as long as CONDITION evaluates to TRUE
Bash while Loop Examples
The below example is a simple example of a while loop. It declares a variable i which will be used to count how many times the loop has run. A for loop is then defined which will run so long as i is less than 10 using the -lt (less than) operator:
#!/bin/bash i=0 while [ $i -lt 10 ] do echo $i ((i++)) done
In each iteration of the loop, the value of i is printed to the console, and i is incremented by 1. The output of this will be:
0 1 2 3 4 5 6 7 8 9
Reading Data from a File Line-By-Line
One common use case for while loops is reading a file line by line:
#!/bin/bash file_path=/path/to/file.txt while read -r line; do echo $line done < "$file_path"
The above code uses redirection (utilizing the < operator) to pass the contents of a text file to the read command. The read command passes the contents of the text file line-by-line to the while loop.
You can also read the options/parameters passed to the current script from the command line in the order they were listed:
#!/bin/bash while getopts ae:f:hd:s:qx: option do echo $option done
Infinite while loops
Infinite loops can be provided by omitting the condition from the while loop:
#!/bin/bash while : do echo "Infinite loop! It never ends (unless you press Ctrl+C)!" done
Infinite loops are useful for repeating tasks in scripts that will be run in the background, like polling a server for information that needs to be kept up to date on-screen.
while Loop One-Liners
While loops can be written on one line. This makes them harder to read, but can be useful if you are using the loop directly in the console rather than from a script:
This is the first example from above, presented as a single line of Bash script:
i=0; while [ $i -lt 10 ]; do echo $i $(( i++ )); done
Breaking (And Continuing) the Loop
If you wish to exit a loop early, use the break statement:
#!/bin/bash i=0 while [ $i -lt 10 ] do echo $i if [[ $i == 3 ]]; then break fi ((i++)) done
The above code will exit the loop prematurely when i reaches 3 using the *break statement.
The continue statement will skip an iteration of the loop:
#!/bin/bash i=0 while [ $i -lt 10 ] do ((i++)) if [[ $i == 3 ]]; then continue fi echo $i done
Above, the iteration of the loop where i is equal to 3 will be skipped, resulting in the following output:
1 2 4 5 6 7 8 9 10
Note that in this example script, i is incremented at the beginning of the code within the loop – otherwise if it were at the end, when the continue statement is called, an (unintended) infinite loop would be created.