< Previous | Contents | Next >
until
The until command is much like while, except instead of exiting a loop when a non- zero exit status is encountered, it does the opposite. An until loop continues until it re- ceives a zero exit status. In our while-count script, we continued the loop as long as the value of the count variable was less than or equal to 5. We could get the same result by coding the script with until:
#!/bin/bash
# until-count: display a series of numbers count=1
until [[ $count -gt 5 ]]; do echo $count
#!/bin/bash
# until-count: display a series of numbers count=1
until [[ $count -gt 5 ]]; do echo $count
Breaking Out Of A Loop
count=$((count + 1)) done
echo "Finished."
count=$((count + 1)) done
echo "Finished."
By changing the test expression to $count -gt 5, until will terminate the loop at the correct time. The decision of whether to use the while or until loop is usually a matter of choosing the one that allows the clearest test to be written.