看来这两个操作符几乎是一样的 – 是否有区别?我应该在什么时候使用=和when ==?
您必须在((…))中的数字比较中使用==:
原文链接:https://www.f2er.com/bash/391213.html$ 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 ")
您可以在[[…]]或[…]中使用字符串比较或测试:
$ 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
“字符串比较?”,你说?
$ 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