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

后端 未结 3 1343
情话喂你
情话喂你 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:03

    -eq, -lt, -gt is only used for arithmetic value comparison(integers).

    == is used for string comparison.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-23 01:15

    Have a look at this explanation of 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.

    0 讨论(0)
提交回复
热议问题