KSH:

编程入门 行业动态 更新时间:2024-10-26 20:22:40
KSH:-eq和=之间的差异(KSH: diference between -eq and =)

假设我有这个小程序:

until [ "$name" = "$secretname" ] do read name done

而不是使用=,如果我使用-eq它仍然有效。

是否有理由在“-eq”上使用“=”

Let say I have this small program:

until [ "$name" = "$secretname" ] do read name done

Instead of using =, if i used -eq it still works.

Is there a reason for using the "=" over the "-eq"

最满意答案

-eq仅用于比较整数。 它将忽略前导零:

$ test 09 -eq 9 && echo match || echo no match match

如果参数不是整数,它也会报告错误:

$ test abc -eq abc && echo match || echo no match bash: test: abc: integer expression expected no match

=用于比较任意字符串

$ test abc = abc && echo match || echo no match match $ test 09 = 9 && echo match || echo no match no match

-eq is only for comparing integers. It will ignore leading zeroes:

$ test 09 -eq 9 && echo match || echo no match match

It will also report an error if the arguments are not integers:

$ test abc -eq abc && echo match || echo no match bash: test: abc: integer expression expected no match

= is for comparing arbitrary strings

$ test abc = abc && echo match || echo no match match $ test 09 = 9 && echo match || echo no match no match

更多推荐

本文发布于:2023-07-22 18:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:KSH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!