Numerical comparisons

suggest change

Numerical 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:

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"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents