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 incrementsi
to 1. - In the second iteration,
i
is 1, and the condition is still false, so it echoes 1 and incrementsi
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:
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.