Bash until Loop | Explained

The until loop in Bash is used for the execution of a block of code repeatedly until a certain condition is met. It is similar to the while loop; however, the until loop continues to execute as long as the condition is false, whereas the while loop continues to execute till the condition is true.

This article will demonstrate the “until” loop in bash script along with syntax and possible examples in Linux. The content that illustrates this guide is mentioned below:

Let’s start the article with the basics of until loop.

How Does until Loop Work in Bash?

In a Bash script, the until loop is utilized with various examples and syntax. The basic syntax for an until loop in Bash:

Syntax:

until condition
do
    # code to be executed
done

Example 1: Counts Down from 10 to 1

Here is an example of an until loop that counts down from 10 to 1. It decreases values one by one until the specific condition is met:

Code

# Initialize the counter variable
counter=10

until [ $counter -eq 0 ]
do
    # Print the current value of the counter
    echo $counter
   
    # Decrement the counter
    ((counter--))
done

The description of the below code is provided in the listed format:

  • First, initialize a “counter” variable with the value 10.
  • After that, “until” the loop is utilized that executes until the condition of the “counter” variable is equal to 0 is met.
  • At each iteration, the “counter” variable displays the current value via “echo”.
  • In the end, the decrement counter is placed that decreases one value from the counter variable at each iteration.

Note: The above code is stored in the “test.sh” file via the nano editor.

To execute the script file, specify the name of the file “test.sh”. The below script displays the output:

$ bash test.sh

The output returns the values 10, 9, 8, 7, 6, 5, 4, 3, 2, and 1 and then terminates when the counter variable reaches 0.

Example 2: Using until Loop with break Statement

Users can use the break and continue statements inside an until loop to control the flow of the loop. The break statement will cause the loop to terminate immediately, while the continue statement will skip the rest of the current iteration and move on to the next one.

Code

counter=10

until [ $counter -eq 0 ]
do
    if [ $counter -eq 5 ]
    then
        # Exit the loop early
        break
    fi
   
    # Print the current value of the counter
    echo $counter
   
    # Decrement the counter
    ((counter--))
done

The until loop is considered in a bash script that counts down from 10 to 1 but exits the loop early if the counter reaches 5. The description of the code is provided below:

  • First, a “counter” variable is initialized with the 10 value.
  • After that, a condition is placed inside the until loop that terminates if the counter value is equal to 0.
  • Another condition is written inside the body of the “until” loop that if the counter value is equal to 5, it terminates the code via a “break” statement.
  • After that, the “echo” command prints the current value of a counter variable at each iteration.
  • In the end, decrement the counter that reduces the total counter value one by one.

To execute the above code, specify the name of the script file “test.sh” in the terminal:

$ bash test.sh

This until loop will print the values 10, 9, 8, 7, 6, and then exit the loop when the counter reaches 5.

Example 3: Using until Loop to Display Message Infinite Time

To display the message infinite time, use the “until” condition with a “false” value. For instance, display the “Hello” message as below:

The output shows that the “Hello” message is displayed in infinite time.

That is all from the until loop in the Bash script.

Conclusion

The until loop iterates the piece of code until the particular condition is met. Using the until loop, users can manipulate conditions according to requirements. This article has provided the basic syntax of until loop, along with multiple examples in the bash script.