什么是运营商QUOT之间的差异; = QUOT;和" =="在Bash的?
问题描述:
看来,这两个运营商pretty大同小异 - 是有区别吗?什么时候应该使用 =
当 ==
?
It seems that these two operators are pretty much the same - is there a difference? When should I use =
and when ==
?
答
您必须使用 ==
在数字比较中((... ))
:
You must use ==
in numeric comparisons in (( ... ))
:
$ 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
字符串比较?,你说呢?
"String comparisons?", you say?
$ 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