简体   繁体   English

Bash -eq和==,区别是什么?

[英]Bash -eq and ==, what's the diff?

Why this works: 工作原理:

Output=$( tail --lines=1 $fileDiProva )
##[INFO]Output = "OK"

if [[ $Output == $OK ]]; then
    echo "OK"
else
    echo "No Match"
fi

and this not? 这不是吗?

Output=$( tail --lines=1 $fileDiProva )
##[INFO]Output = "OK"

if [[ $Output -eq $OK ]]; then
    echo "OK"
else
    echo "No Match"
fi

What's the difference?? 有什么不同?? between == and -eq? 在==和-eq之间?

Thanks! 谢谢!

-eq is an arithmetic test. -eq是一种算术测试。

You are comparing strings. 您正在比较字符串。

From help test : help test

Other operators:

  arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                 -lt, -le, -gt, or -ge.

When you use [[ and use -eq as the operator, the shell attempts to evaluate the LHS and RHS. 当使用[[并使用-eq作为运算符时,shell会尝试评估LHS和RHS。 The following example would explain it: 以下示例将对其进行解释:

$ foo=something
+ foo=something
$ bar=other
+ bar=other
$ [[ $foo -eq $bar ]] && echo y
+ [[ something -eq other ]]
+ echo y
y
$ something=42
+ something=42
$ [[ $foo -eq $bar ]] && echo y
+ [[ something -eq other ]]
$ other=42
+ other=42
$ [[ $foo -eq $bar ]] && echo y
+ [[ something -eq other ]]
+ echo y
y

Have a look at this explanation of if . 看一下if的解释

The first one == is in the section of string compare operators and it can only compare two strings. 第一个==位于字符串比较运算符部分,它只能比较两个字符串。

The second one -eq is in the last section ARG1 OP ARG2 (last one) and its documentation says "ARG1" and "ARG2" are integers . 第二个-eq在最后一部分ARG1 OP ARG2 (最后一个),其文档说"ARG1" and "ARG2" are integers

-eq , -lt , -gt is only used for arithmetic value comparison(integers). -eq-lt-gt仅用于算术值比较(整数)。

== is used for string comparison. ==用于字符串比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM