while loop in a Linux Bash script is a type of loop that continues to execute as long as the programmed condition remains true.
while loops are useful when you need to repeatedly execute a set of instructions a certain number of times, or when you want to create an infinite loop. In this tutorial, you will see various examples of
while loops in a Bash script so you can learn how they are written and what kind of purpose they serve.
In this tutorial you will learn:
- How to make a
whileloop repeat a certain number of times
- How to create an infinite
- How to use
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Software||Bash shell (installed by default)|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Bash script: While loop examples
- A very typical use for a
whileloop would be to repeat the same portion of a script a certain number of times. We can use an incrementing variable to control how many times a script is executed. Take the following script for an example which is a simple 5 second countdown timer script.
#!/bin/bash i=5 while [ $i -gt 0 ] do echo Countdown ends in $i... ((i--)) sleep 1 done echo Countdown is over!
$istarts out with a value of 5. Whenever this variable is greater than 0, the while
loopcondition will be true and the loop will execute. In this case, the loop will execute five times, before the
$ivariable has been decremented down to 0. This effectively creates a 5 second countdown timer. Here is the output from our terminal when we execute the script:
$ ./test.sh Countdown ends in 5... Countdown ends in 4... Countdown ends in 3... Countdown ends in 2... Countdown ends in 1... Countdown is over!
whileloops are also useful when you want to create an infinite loop. For example, if you want a loop to continue executing until the user manually hits
Ctrl + Cor otherwise kills the script. Or if you have a system administration script that continually checks for disk space or some other stat, for example.
#!/bin/bash i=1 while : do echo Countdown to infinity: $i... ((i++)) sleep 0.1s done
The above is a basic script that will continuously count until the script is killed. This loop never ends because the condition is always true. Rather than writing some contrived condition that would always be true (like while 2 is greater than 1), we can just write
while :. Here is the output from our terminal when we execute the script:
$ ./test.sh Countdown to infinity: 1... Countdown to infinity: 2... Countdown to infinity: 3... Countdown to infinity: 4... Countdown to infinity: 5... ^C
breakcommand can be used in a
whileloop in order to break out of the loop and stop it prematurely. Usually
breakwould be used when a certain condition is met, such as with an
ifstatement. Let’s return to our previous “countdown to infinity” example but add a
#!/bin/bash i=1 while : do if [ $i -eq 6 ]; then break fi echo Countdown to infinity: $i... ((i++)) sleep 0.1s done echo Countdown is over.
In this case, our countdown is stopped whenever the variable is equal to 6, which should take exactly five iterations through the loop. After the
breakis triggered, the script moves on to whatever comes after the
whileloop, which in this case is just an
echocommand. Here is the output from our terminal when we execute the script:
$ ./test.sh Countdown to infinity: 1... Countdown to infinity: 2... Countdown to infinity: 3... Countdown to infinity: 4... Countdown to infinity: 5... Countdown is over.
- We can also use the
continuecommand to break out of a
whileloop for the current iteration, yet continue to execute the loop (as long as the condition is still true). This works like
break, but instead of moving on to the next part of the script, it goes back for another loop.
#!/bin/bash i=0 while : do ((i++)) if [ $i -ge 6 ] && [ $i -le 19 ]; then continue fi echo Countdown to infinity: $i... sleep 0.1s done
In this example, the
continueis triggered as long as the
$ivariable is at a value between 6 and 19. This way, our countdown to infinity timer will skip from 5 to 20, as seen in the output below. The
continuecommand allows us to escape from the
whileloop prematurely, but move back to the top of the loop instead of on to the next part of the script. Here is the output from our terminal when we execute the script:
$ ./test.sh Countdown to infinity: 1... Countdown to infinity: 2... Countdown to infinity: 3... Countdown to infinity: 4... Countdown to infinity: 5... Countdown to infinity: 20... Countdown to infinity: 21... ^C
In this tutorial, you saw how to use
whileloops in a Bash script on a Linux system. This included typical
whileloops, as well as infinite
whileloops, and even loops that featured the
continuecommands. This should be all you need to quickly master these handy loops on Linux, allowing you to write your own or adapt our examples to get started.