while loops and until loops
When using the for
loop, we iterate through a list; it's either the one that we create or the one that is dynamically generated. Using the while
or until
loops, we loop based on the fact that the condition becomes either true
or false
.
A while
loop loops while the condition is true and, conversely, an until
loop will loop while the condition is false. The following command will count from 10 through to zero, each iteration of the loop printing the variable and then reducing the value by one:
$ COUNT=10$ while (( COUNT >= 0 )) ; doecho -e "$COUNT \c"(( COUNT-- ))done ; echo
We can see the output of this command in the following screenshot, thus confirming the countdown to zero:

Note
The use of the\c
escape sequence used here allows the suppression of the line feed normally used with echo
. In this way, we can keep the countdown on the single line of output. I think you will agree that it's a nice effect.
The functionality of this loop can be gained using the until
...