什么是运营商QUOT之间的差异; = QUOT;和" =="在Bash的? [英] What is the difference between operator "=" and "==" in Bash?

查看:160
本文介绍了什么是运营商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

这篇关于什么是运营商QUOT之间的差异; = QUOT;和&QUOT; ==&QUOT;在Bash的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆