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

后端 未结 3 1342
情话喂你
情话喂你 2021-01-23 00:31

Why this works:

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

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


        
3条回答
  •  时光说笑
    2021-01-23 01:15

    -eq is an arithmetic test.

    You are comparing strings.

    From 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. 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
    

提交回复
热议问题