2010-04-08 29 views

cevap

71

Sen ((...)) sayısal karşılaştırmalarda == kullanmalısınız:

$ if ((3 == 3)); then echo "yes"; fi 
yes 
$ if ((3 = 3)); then echo "yes"; fi 
bash: ((: 3 = 3 : attempted assignment to non-variable (error token is "= 3 ") 

Sen [[ ... ]] veya [ ... ] veya test dize karşılaştırmaları için birini kullanabilirsiniz:

$ if [[ 3 == 3 ]]; then echo "yes"; fi 
yes 
$ if [[ 3 = 3 ]]; then echo "yes"; fi 
yes 
$ if [ 3 == 3 ]; then echo "yes"; fi 
yes 
$ if [ 3 = 3 ]; then echo "yes"; fi 
yes 
$ if test 3 == 3; then echo "yes"; fi 
yes 
$ if test 3 = 3; then echo "yes"; fi 
yes 

"Dize karşılaştırmaları?" diyorsun?

$ if [[ 10 < 2 ]]; then echo "yes"; fi # string comparison 
yes 
$ if ((10 < 2)); then echo "yes"; else echo "no"; fi # numeric comparison 
no 
$ if [[ 10 -lt 2 ]]; then echo "yes"; else echo "no"; fi # numeric comparison 
no 
+3

'' = '' ile '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' 'ile kullanmamalısınız '==', POSIX belirtiminin bir parçası değildir ve tüm kabuklarla çalışmayacaktır (özellikle çizgi, bunu algılamaz). – chepner

+3

@chepner: Bu doğru, ama soru özellikle Bash hakkında. –

29

POSIX ile ilgili ince bir fark var. Bash reference alıntı: dizeleri eşitse

string1 == string2
doğrudur. Sık POSIX uyumluluğu için == yerine = kullanılabilir.

+0

Yine de fark yok mu? Sadece bir taşınabilirlik sorunu mu? –

+0

@ T.E.D .: Hayır, cevabımı gör. –

İlgili konular