You are viewing a single comment's thread. Return to all comments →
Yes, the chart is incorrect. man test shows the correct operators.
Technically, = is for strings, and -eq is for integers. Using = on integers compares them as strings, since bash is weakly typed. Often this won't have much of an impact, but you can see the difference by testing [ 0 = 00 ] vs [ 0 -eq 00 ].
[ 0 = 00 ]
[ 0 -eq 00 ]
Best practice is to use the correct operator so that when you hit the weird edge cases, you don't lose hair looking for a hard-to-find bug.
Shane, you are right in general, but there's exceptions to your sentence.
Firstly, In test comparisson, it's better use double square brackets rather than one: if [[ $a -ne "0" ]] fi
more info: conditional expression
Secondly, you also can compare integers using == like in this example:
Note that in Bash you don't need the arithmetic expansion to check for the boolean value of an arithmetic expression. This can be done using the arithmetic evaluation compound command:
printf %s 'Enter a number: ' >&2
read -r number
if ((number == 1234)); then
echo 'Good guess'
echo 'Haha... :-P'
more info: Arithmetic expansion
But at the end of the day is up to the programmer to use this functionality or not.
For example this answer is correct:
if (($x == $y)) && (($y == $z)) && (($z == $x)); then
elif (($x != $y)) && (($y != $z)) && (($z != $x)); then