You are viewing a single comment's thread. Return to all comments →
I could not help but notice that there seems to be an issue while trying to use "-neq" as the "not equal" operator.
I am now seeing that the correct "not equal" operator should be either "!=" or "-ne", but it says that the "not equal" operator should be "-neq", according to the chart provided in the bash shell guide provided: http://www.panix.com/~elflord/unix/bash-tute.html (underneath the section entitled "A brief summary of test operators").
Just thought I'd point out the typo! :) It might throw off other beginners like me.
(Anyway, is there any real difference between "=" and "-eq", other than that you cannot use "-eq" on strings? Just wondering if anyone knows off the top of their head.)
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
Thank you very much !