Shell表達式中的 [ 和 [[ 的區別
AAA="eee rrr"
if [ "$AAA" == "eee rrr" ]
then
echo "equal!" # runs here.
else
echo "not equal!"
fi
AAA="eee rrr"
if [[ $AAA == "eee rrr" ]]
then
echo "equal!" # runs here
else
echo "not equal!"
fi
AAA="eee rrr"
if [ $AAA == "eee rrr" ] # [: too many arguments
then
echo "equal!"
else
echo "not equal!" # runs here
fi
可見,如果變量包含空格,即使 $AAA 兩邊不帶雙引號,使用[[ 可以防止變量被分割,當然,始終使用雙引號是一個好習慣。如果使用[ ,則變量必須加上引號。