Why equal to operator does not work if it is not surrounded by space?

前端 未结 4 2041
时光取名叫无心
时光取名叫无心 2020-11-22 14:29

I tried the following script

#!/bin/bash
var1=\"Test 1\" 
var2=\"Test 2\"
if [ \"$var1\"=\"$var2\" ] 
  then 
    echo \"Equal\" 
  else 
    echo \"Not equa         


        
相关标签:
4条回答
  • 2020-11-22 14:44

    To add to the existing explanation, "$var1"="$var2" is just a single non-empty string, and thus always evaluates as true in a conditional.

    [ "$var1"="$var2" ] && echo true
    

    The above command will always print out true (even if var1 and var2 be empty).

    0 讨论(0)
  • 2020-11-22 14:52

    test (or [ expr ]) is a builtin function. Like all functions in bash, you pass it's arguments as whitespace separated words.

    As the man page for bash builtins states: "Each operator and operand must be a separate argument."

    It's just the way bash and most other Unix shells work.

    Variable assignment is different.

    In bash a variable assignment has the syntax: name=[value]. You cannot put unquoted spaces around the = because bash would not interpret this as the assignment you intend. bash treats most lists of words as a command with parameters.

    E.g.

    # call the command or function 'abc' with '=def' as argument
    abc =def
    
    # call 'def' with the variable 'abc' set to the empty string
    abc= def
    
    # call 'ghi' with 'abc' set to 'def'
    abc=def ghi
    
    # set 'abc' to 'def ghi'
    abc="def ghi"
    
    0 讨论(0)
  • 2020-11-22 14:58

    In bash the best is to use [[ ]]:

    x="test"
    y="test"
    if [[ "${x}" = "${y}" ]]; then
        echo "Equals"
    else
        echo "No equals"
    fi
    
    0 讨论(0)
  • 2020-11-22 15:07

    When the shell reads

    if [ "$var1" = "$var2" ]
    

    it invokes the command [ with 4 arguments. Whether [ is a builtin or an external command is irrelevant, but it may help to understand that it may be the external command /bin/[. The second argument is the literal '=' and the fourth is ']'. However, when the shell reads

    if [ "$var1"= "$var2" ]
    

    [ only gets 3 arguments: the expansion of $var1 with '=' appended, the expansion of $var2, and ']'. When it gets only 3 arguments, it expects the last argument to be ']' and the first argument to be a unary operator.

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