运算符“=”和“==”在Bash中有什么区别?

看来这两个运营商几乎是一样的 – 有没有区别? 什么时候应该使用=和什么时候==

(( ... ))中的数字比较必须使用==

 $ 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 ") 

您可以在[[ ... ]] [ ... ]或者test使用string比较:

 $ 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 

“string比较?”,你说?

 $ 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 

关于POSIX有一个细微的差别。 摘自Bash的参考文献 :

string1 == string2
如果string相等,则为真。 =可以用来代替==严格遵守POSIX。