Open In App

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.

Syntax of `Until` Loop

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

Here, the flow of the above syntax will be –



Example:

#!/bin/bash
echo "until loop"
i=10
until [ $i == 1 ]
do
    echo "$i is not equal to 1";
    i=$((i-1))
done
echo "i value is $i"
echo "loop terminated"

In this example, the script starts with the value of i set to 10. The “until” loop checks the condition [ $i == 1 ] before executing the block of statements inside the loop.

Output:



until loop

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.

Program:

#!/bin/bash
condition=false
iteration_no=0
until $condition
do
    echo "Iteration no : $iteration_no"
    ((iteration_no++))
    sleep 1
done

The script starts by setting the variable condition to false, which means the condition used in the “until” loop will always be false.

Since the condition $condition is always false, the loop will never terminate. It will keep printing the iteration number and incrementing it indefinitely, causing an infinite loop.

If you run this script in your terminal, you’ll see it continuously printing the iteration number without stopping until you manually interrupt the script (for example, by pressing Ctrl+C).

Output:

Infinite loop

Until Loop with break and continue

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

Program

#!/bin/bash
count=1

# this is an infinite loop
until false
do
    
    if [[ $count -eq 25 ]]
    then
    
        ##   terminates the loop.
        break
    elif [[ $count%5 -eq 0 ]]
    then
    
        ## terminates the current iteration.
        continue
    fi
    echo "$count"
    ((count++))
done

The script starts the infinite “until” loop.

In each iteration, it checks the conditions:

The loop continues until count becomes 25. At that point, the “break” statement is executed, and the loop terminates.

Output:

Break continue statement

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:

Output:

Single 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.

Program

#!/bin/bash
n=1
sum=0
until [[ $n -gt 15  || $sum -gt 20 ]]
do
    sum=$(($sum + $n))
    echo "n = $n & sum of first n = $sum"
    ((n++))
done

Now, let’s see how the script behaves:

Output:

Multiple loop

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.


Article Tags :