Quando dovrei usare -eqvs =vs==
per esempio
[[ $num -eq 0 ]]
[[ $num = 'zzz' ]]
Ho osservato un modello di utilizzo -eq(e -ne, ecc.) Per i numeri e =per le stringhe. C'è una ragione per questo e quando dovrei usare==
Quando dovrei usare -eqvs =vs==
per esempio
[[ $num -eq 0 ]]
[[ $num = 'zzz' ]]
Ho osservato un modello di utilizzo -eq(e -ne, ecc.) Per i numeri e =per le stringhe. C'è una ragione per questo e quando dovrei usare==
Risposte:
Perché questa è la definizione per quegli operandi. Dalla documentazione di test 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; altrimenti, falso.
==non è definito da POSIX, è un'estensione di bash, derivata da ksh. Non dovresti usare ==quando vuoi la portabilità. Dalla documentazione di bash - Espressioni condizionali di Bash :
string1 == string2
string1 = string2
Vero se le stringhe sono uguali. '=' deve essere usato con il comando test per la conformità POSIX.
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.
Da man test:
-eq , eccetera.
Relè a test aritmetici. Gli argomenti devono essere interamente numerici (possibilmente negativi), oppure l'espressione speciale `-l STRING ', che valuta 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
Il simbolo =viene utilizzato per il confronto delle stringhe mentre -eqper il confronto dei numeri interi. Entrambi lavorano con teste con [...]. Se si utilizza bashquindi con la sintassi [[...]]è possibile utilizzare anche ==per il confronto delle stringhe. Inoltre in bash =e ==con [[...]]lavori patternsanche per (come ad esempio [[ $x == y* ]].