unix test when to use eq vs = vs == in test commands?

23,696

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

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

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

Solution 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

Share:
23,696

Related videos on Youtube

Michael Durrant
Author by

Michael Durrant

rails ruby rspec rock

Updated on September 18, 2022

Comments

  • Michael Durrant
    Michael Durrant almost 2 years

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