Bash script: While loop examples

The 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 while loop repeat a certain number of times
  • How to create an infinite while loop
  • How to use continue and break in a while loop
An example of using a while loop in a Bash script on Linux
An example of using a while loop in a Bash script on Linux
Software Requirements and Linux Command Line Conventions
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 sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash script: While loop examples



  1. A very typical use for a while loop 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!

    The variable $i starts out with a value of 5. Whenever this variable is greater than 0, the while loop condition will be true and the loop will execute. In this case, the loop will execute five times, before the $i variable 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!
    
  2. while loops 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 + C or 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
    
  3. The break command can be used in a while loop in order to break out of the loop and stop it prematurely. Usually break would be used when a certain condition is met, such as with an if statement. Let’s return to our previous “countdown to infinity” example but add a break in it:


    #!/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 break is triggered, the script moves on to whatever comes after the while loop, which in this case is just an echo command. 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.
    
  4. We can also use the continue command to break out of a while loop 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 continue is triggered as long as the $i variable 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 continue command allows us to escape from the while loop 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
    

Closing Thoughts




In this tutorial, you saw how to use while loops in a Bash script on a Linux system. This included typical while loops, as well as infinite while loops, and even loops that featured the break and continue commands. 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.



Comments and Discussions
Linux Forum