buzzinolops buzzinolops - 6 months ago 8
Bash Question

"if" and "until" give different results in bash scripting

Why is it that I get different results depending on if I have an

until
statement or an
if
statement?

# compare dates

a=12
b=8

if [ $a -le $b ]; then
echo "it is not less than using if"
fi

until [ $a -le $b ]
do
echo "it is less than using until !?!?!?"
done


The
if
statement fails, but the
until
statement prints forever, even though they are the exact same condition. Why?

Thank you. This will really help my understanding.

Answer

even though they are the exact same condition

This is incorrect. The if statement says "execute the following if the condition is true". The until statement says "execute the following if the condition is false (and keep executing it as long as the condition remains false)".

You may be thinking of the while statement. The while statement says "execute the following if the condition is true (and keep executing it as long as the condition remains true)".

Comments