Quando devo utilizzare -eq vs = vs ==
ad es
[[ $num -eq 0 ]] [[ $num = "zzz" ]]
Ho “osservato uno schema di utilizzo di -eq (e -ne, ecc.) per i numeri e = per le stringhe. Cè una ragione per questo e quando dovrei usare ==
Risposta
Perché questa è la definizione di quegli operandi. Dalla documentazione di prova POSIX, sezione OPERANDS :
s1 = s2
Vero se le stringhe s1 e s2 sono identiche; altrimenti, falso.
…
n1 -eq n2
Vero se gli interi n1 e n2 sono algebricamente uguali; in caso contrario, false.
== non è definito da POSIX, è unestensione di bash, derivato da ksh. Non dovresti “utilizzare == quando desideri la portabilità. Da documentazione bash – Espressioni condizionali Bash :
string1 == string2
string1 = string2
Vero se le stringhe sono uguali. = deve essere utilizzato con il comando di prova per la conformità POSIX.
Risposta
In un modo più elaborato
Le seguenti sequenze possono aiutare:
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.
Risposta
Da man test:
-eq e così via
Relay ai test aritmetici. Gli argomenti devono essere interamente numerici (possibilmente negativi) o lespressione speciale “-l STRING”, che restituisce la lunghezza di STRING.
STRING1 = STRING2
True if the strings are equal.
STRING1 == STRING2
True if the strings are equal (synonym for =).
Quindi = e == sono sinonimi
Risposta
Il simbolo = viene utilizzato per il confronto delle stringhe mentre -eq per il confronto dei numeri interi. Entrambi funzionano con test e con [...]. Se utilizzi bash con la sintassi [[...]] puoi utilizzare anche == per il confronto delle stringhe. Inoltre in bash = e == con funziona anche per patterns (come ad esempio [[ $x == y* ]].