Until loop
From Linux Shell Scripting Tutorial - A Beginner's handbook
Just like while loop, until loop is also based on a condition.
Syntax
The until loop continues running commands as long as the item in list continues to evaluate true. Once an item evaluates false, the loop is exited. The syntax is:
until [ condition ] do command1 command2 ... .... commandN done
The while loop vs the until loop
- The until loop executes until a nonzero status is returned.
- The while command executes until a zero status is returned.
- The until loop always executes at least once.
Example
Create a shell script called until.sh:
#!/bin/bash i=1 until [ $i -gt 6 ] do echo "Welcome $i times." i=$(( i+1 )) done
Save and close the file. Run it as follows:
chmod +x until.sh ./until.sh
Sample outputs:
Welcome 1 times. Welcome 2 times. Welcome 3 times. Welcome 4 times. Welcome 5 times. Welcome 6 times.
The loop in the above example initializes the variable i to 1, and then increments and displays out the message until it equals 6.