Unix的相等运算符(==,-eq) [英] Unix Equality Operators ( == , -eq )
本文介绍了Unix的相等运算符(==,-eq)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以解释bash脚本编程当量和==之间的区别?
Can someone please explain the difference between -eq and == in bash scripting?
有以下任何区别:
[$一个-eq $ B]
和 [$ A == $ B]
难道仅仅当变量包含数字,==只能使用吗?
Is it simply that == is only used when the variables contain numbers?
推荐答案
这是围绕otherway: ==
是字符串比较,当量
是数字的。 -eq
是在同一个家庭为 -lt
, -le
, -gt
, -ge
和 -ne
,如果这能帮助你记住哪个是哪个。
It's the otherway around: ==
is for string comparisons, -eq
is for numeric ones. -eq
is in the same family as -lt
, -le
, -gt
, -ge
, and -ne
, if that helps you remember which is which.
$ [ a == a ]; echo $?
0
$ [ a -eq a ]; echo $?
-bash: [: a: integer expression expected
2
==
是一个bash主义。 POSIX的形式是 =
。如果移植到非bash的外壳是很重要的,使用 =
。
==
is a bash-ism. The POSIX form is =
. If portability to non-bash shells is important, use =
.
这篇关于Unix的相等运算符(==,-eq)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文