加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 16:27:17 所属栏目:安全 来源:网络整理
导读:看来这两个操作符几乎是一样的 – 是否有区别?我应该在什么时候使用=和when ==? 您必须在((…))中的数字比较中使用==: $ if (( 3 == 3 )); then echo "yes"; fiyes$ if (( 3 = 3 )); then echo "yes"; fibash: ((: 3 = 3 : attempted assignment to non-v
看来这两个操作符几乎是一样的 – 是否有区别?我应该在什么时候使用=和when ==?
您必须在((…))中的数字比较中使用==:
$ 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读