Numerical comparisons
suggest changeNumerical comparisons use the -eq operators and friends
if [[ $num1 -eq $num2 ]]; then
echo "$num1 == $num2"
fi
if [[ $num1 -le $num2 ]]; then
echo "$num1 <= $num2"
fi
There are six numeric operators:
-eqequal-nenot equal-leless or equal-ltless than-gegreater or equal-gtgreater than
Note that the \< and \> operators inside [[ … ]] compare strings, not numbers.
if [[ 9 -lt 10 ]]; then
echo "9 is before 10 in numeric order"
fi
if [[ 9 > 10 ]]; then
echo "9 is after 10 in lexicographic order"
fi
The two sides must be numbers written in decimal (or in octal with a leading zero). Alternatively, use the ((…)) arithmetic expression syntax, which performs integer calculations in a C/Java/…-like syntax.
x=2
if ((2*x == 4)); then
echo "2 times 2 is 4"
fi
((x += 1))
echo "2 plus 1 is $x"
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents