When should I use -eq vs = vs ==
e.g.
[[ $num -eq 0 ]] [[ $num = 'zzz' ]]
I’ve observed a pattern of using -eq (and -ne, etc.) for numbers and = for strings. Is there a reason for this and when should I use ==
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Because that’s the definition for those operands. From POSIX test documentation, OPERANDS section:
s1 = s2
True if the strings s1 and s2 are identical; otherwise, false.
…
n1 -eq n2
True if the integers n1 and n2 are algebraically equal; otherwise,
false.
== is not defined by POSIX, it’s an extension of bash, derived from ksh. You shouldn’t use == when you want portability. From bash documentation – Bash Conditional Expressions:
string1 == string2
string1 = string2
True if the strings are equal. ‘=’ should be used with the test
command for POSIX conformance.
Method 2
Symbol = is used for string comparison while -eq for integer comparison. Both work with test and with [...]. If you are using bash then with syntax [[...]] you can use also == for string comparison. Additionally in bash = and == with [[...]]works for patterns too (as for example [[ $x == y* ]].
Method 3
In a more elaborate way
Following sequences can help :
gnu:~$ [ sam -eq sam ] bash: [: sam: integer expression expected gnu:~$ echo "Exit status of "[ sam -eq sam ]" is $?." Exit status of "[ sam -eq sam ]" is 2.
gnu:~$ [ 5 -eq 5 ] gnu:~$ echo "Exit status of "[ 5 -eq 5 ]" is $?." Exit status of "[ 5 -eq 5 ]" is 0.
gnu:~$ [ 5 = 5 ] gnu:~$ echo "Exit status of "[ 5 = 5 ]" is $?." Exit status of "[ 5 = 5 ]" is 0.
gnu:~$ [ sam = sam ] gnu:~$ echo "Exit status of "[ sam = sam ]" is $?." Exit status of "[ sam = sam ]" is 0.
gnu:~$ [ 5 == 5 ] gnu:~$ echo "Exit status of "[ 5 == 5 ]" is $?." Exit status of "[ 5 == 5 ]" is 0.
gnu:~$ [ sam == sam ] gnu:~$ echo "Exit status of "[ sam == sam ]" is $?." Exit status of "[ sam == sam ]" is 0.
gnu:~$ (( 5 == 5 )) gnu:~$ echo "Exit status of "(( 5 == 5 ))" is $?." Exit status of "(( 5 == 5 ))" is 0.
gnu:~$ (( sam == sam )) gnu:~$ echo "Exit status of "(( sam == sam ))" is $?." Exit status of "(( sam == sam ))" is 0.
gnu:~$ ( sam = sam ) The program 'sam' is currently not installed. You can install it by typing: sudo apt-get install simon gnu:~$ echo "Exit status of "( sam = sam )" is $?." Exit status of "( sam = sam )" is 127.
gnu:~$ ( 5 = 5 ) 5: command not found gnu:~$ echo "Exit status of "( 5 = 5 )" is $?." Exit status of "( 5 = 5 )" is 127.
gnu:~$ ( sam == sam ) The program 'sam' is currently not installed. You can install it by typing: sudo apt-get install simon gnu:~$ echo "Exit status of "( sam == sam )" is $?." Exit status of "( sam == sam )" is 127.
gnu:~$ ( 5 == 5 ) 5: command not found gnu:~$ echo "Exit status of "( 5 == 5 )" is $?." Exit status of "( 5 == 5 )" is 127.
Method 4
From man test:
-eq , etc.
Relay to arithmetic tests. The arguments must be entirely numeric
(possibly negative), or the special expression `-l STRING’, which
evaluates to the length of STRING.
STRING1 = STRING2
True if the strings are equal.
STRING1 == STRING2
True if the strings are equal (synonym for =).
So = and == are synonyms
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0