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

比较bash中的数字时,为什么“[[10 <2]]”为真?

发布时间:2020-12-15 17:03:19 所属栏目:安全 来源:网络整理
导读:参见英文答案 Comparing numbers in Bash7个 Bash – testing if a string is ‘greater’ than another – how does it work internally?3个 Compare variable with integer in shell? 2个 我跑的时候 if [[ 10 2 ]]; then echo "yes";else echo "no"; fi
参见英文答案 > Comparing numbers in Bash7个
> Bash – testing if a string is ‘greater’ than another – how does it work internally?3个
> Compare variable with integer in shell? 2个
我跑的时候
if [[ 10 < 2 ]];
  then echo "yes";
else echo "no"; 
fi

在shell中,它返回yes.为什么?应该不是吗?
而当我跑

if [[ 20 < 2 ]];
  then echo "yes";
else echo "no";
fi

它返回否.

因为您根据 Lexicographical order比较字符串而不是数字

您可以使用[[10 -lt 2]]和[[20 -lt 2]]. -lt代表小于(<).对于大于(>)-gt,可以使用-gt表示法.

在bash中,可以使用双括号来执行数字比较:

if ((10 < 2)); then echo "yes"; else echo "no"; fi

上面的例子将回应没有

(编辑:李大同)

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

    推荐文章
      热点阅读