22

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 ==

24

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.

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.  
2

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

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* ]].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.