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

前端 未结 4 2096
时光取名叫无心
时光取名叫无心 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).

提交回复
热议问题