Bash Scripting - Until Loop
Last Updated :
12 Sep, 2024
'Bash' provides several looping constructs to control the execution flow in scripts, including 'for', 'while', and 'until' loops. The 'until' loop is a unique looping mechanism that runs a block of code repeatedly until a specified condition becomes true. It essentially works in the opposite manner of the 'while' loop, making it a valuable tool when executing commands as long as a condition remains false.
Syntax of `Until`Loop
until [ condition ];
do
block-of-statements
done
Here, the flow of the above syntax will be -
- The condition is evaluated at the start of each iteration.
- If the condition is false, the block of statements inside the loop is executed.
- After executing the block, the loop returns to check the condition again.
- The loop continues until the condition becomes true. Once the condition is true, the control moves to the next command after the loop.
Example 1: Simple Until Loop
The following example demonstrates a basic use of the 'until' loop:
Program:
#!/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.
- At the first iteration,
i
is 10, and the condition [ $i == 1 ]
is false, so it enters the loop and executes echo "$i is not equal to 1"
, which displays "10 is not equal to 1". - Then, it decreases the value of
i
by 1 using i=$((i-1))
, so i
becomes 9, and it goes back to check the condition again. - This process repeats until the value of
i
becomes 1. - At the point where
i
is 1, the condition [ $i == 1 ]
evaluates to true, and the loop terminates. It skips the block of statements inside the loop and proceeds to the next command in the script. - Finally, it prints the value of
i
which is now 1, and then loop terminates.
Output:
until loopExample 2: 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.
'iteration_no'
is set to 0, and it will be used to keep track of the number of iterations.- The "until" loop is defined with the condition
$condition
. Since $condition
is false
, the loop will continue executing indefinitely. - Inside the loop, it first echoes the current iteration number using
echo "Iteration no : $iteration_no"
. - Then,
((iteration_no++))
increments the value of 'iteration_no'
by 1, so the script keeps track of the number of iterations. 'sleep 1'
introduces a one-second delay in each iteration. This sleep is not necessary for the infinite loop itself but can be used to control the pace of iterations and prevent the loop from running too quickly.
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
Example 3: Until Loop with break and continue
This example uses the break and the continue statements to alter the flow of the loop.
- break: This statement terminates the current loop and passes the program control to the following commands.
- continue: This statement ends the current iteration of the loop, skipping all the remaining commands below it and starting the next iteration.
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:
- If
count
is equal to 25, it breaks out of the loop immediately. - If
count
is a multiple of 5, it skips the rest of the commands and starts the next iteration. - If neither condition is met, it echoes the current value of
count
.
The loop continues until count
becomes 25. At that point, the "break" statement is executed, and the loop terminates.
Output:
Break continue statement
Example 4: 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
Example 5: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:
- The script starts the "until" loop with
n
and sum
set to 1. - In the first iteration, it calculates the sum of values from 1 to 1, which is 1, and echoes
n = 1 & sum of first n = 1
. - It increments
n
to 2 and goes to the next iteration. - This process continues until either
n
becomes greater than 15 or the sum becomes greater than 20. - The loop stops when
n
is 16 because the first part of the condition ($n -gt 15
) becomes true, and the loop terminates.
Output:
Multiple loopExit status of a command
The 'until' loop runs as long as the condition returns a non-zero exit status. In Bash, an exit status of '0' indicates success, while any non-zero value indicates failure. Therefore, the 'until' loop continues to execute its block of commands until the exit status changes to '0', effectively acting on failures rather than successes.
Conclusion
The 'until' loop in Bash is a flexible and powerful tool for executing code blocks based on specific conditions. By mastering its use, you can enhance your scripts to handle various scenarios effectively, from basic iteration to complex logic control with multiple conditions. Understanding how to leverage the 'until' loop alongside other Bash constructs like break and continue can significantly improve your scripting skills and make your code more powerful and maintainable.