基本上,[是指向名为test的外部程序的软链接,因此条件作为程序参数传递给它,如果不将$variable用“$quotes”括起来,并且变量恰好为空,则不会将其视为空参数,而是将其视为没有争论(没有)
#!/bin/bash -eu
var=bla
if [[ $var == bla ]];then
echo first test ok
fi
var=""
if [[ $var == "" ]];then
echo second test ok
fi
if [ "$var" == "" ];then
echo third test ok
fi
if [ x$var == "x" ];then
echo fourth test ok
fi
echo this will fail:
if [ $var == "" ];then
echo fifth test ok
fi
echo because it is the same as writing:
if [ == "" ];then
echo sixth test is obviously eroneous
fi
echo but also you should quote your variables because this will work:
var="a b"
if [ "$var" == "a b" ];then
echo seventh test ok
fi
echo ... but this one won\'t as test now has four arguments:
if [ $var == "a b" ];then
echo eighth test ok
fi