Difference between "while" and "until" in Bash
Btw:
COUNTER=20
while [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
This will not execute forever. Counter (20) is not less than 10 during the first evaluation of the loop condition so it will terminate immediately.
They are opposite:
bool b;
while(b) = until(!b)
While Loop executes the block of code (enclosed in do...done) when the condition is true and keeps executing that till the condition becomes false. Once the condition becomes false, the while loop is terminated.
Until Loop executes the block of code (enclosed in do...done) when the condition is false and keep executing that till the condition becomes true. Once the condition becomes true, the until loop is terminated.
COUNTER=20
while [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
On the very first occasion, the condition mentioned in while, i.e. [ $COUNTER -lt 10 ], holds false, so the block of code inside the while loop will not run at all.
while
runs the loop while the condition is true. until
runs the loop until the condition is true (i.e. while the condition is false).
See http://www.gnu.org/software/bash/manual/bashref.html#Looping-Constructs.