Until Loop with Single condition

This is an example of a until loop that checks for only one condition.

Program

#!/bin/bash
i=0
until [[ $i -eq 5 ]]
do
    echo "$i"
    ((i++))
done

Now, let’s see how the script behaves:

  • The script starts the “until” loop with i set to 0.
  • In the first iteration, i is 0, and the condition [[ $i -eq 5 ]] is false, so it echoes 0 and increments i to 1.
  • In the second iteration, i is 1, and the condition is still false, so it echoes 1 and increments i to 2.
  • This process continues until i becomes 5.
  • When i is 5, the condition [[ $i -eq 5 ]] becomes true, and the loop terminates.

Output:

Single condition

Bash Scripting – Until Loop

The Bash has three types of looping constructs namely for, while, and until. The Until loop is used to iterate over a block of commands until the required condition is false.

Similar Reads

Syntax of `Until` Loop

until [ condition ]; do block-of-statements done...

Infinite Loop using Until

In this example the until loop is infinite i.e it runs endlessly. If the condition is set in until the loop is always false then, the loop becomes infinite....

Until Loop with break and continue

This example uses the break and the continue statements to alter the flow of the loop....

Until Loop with Single condition

This is an example of a until loop that checks for only one condition....

Until Loop with Multiple conditions

Until loop can be used with multiple conditions. We can use the and : ‘&&’ operator and or: ‘||’ operator to use multiple conditions....

Exit status of a command:

We know every command in the shell returns an exit status. The exit status of 0 shows successful execution while a non-zero value shows the failure of execution. The until loop executes till the condition returns to a non-zero exit status....