上一段代码:
#!/bin/bash
if [ -n $string1 ]
then
echo "It is not null"
else
echo "This is null"
fi
#exit 0
echo "--------------------------"
string=""
if [ -n "$string1" ]
then
echo "It is not null"
else
echo "This is null"
fi
echo "**************************"
if [ -n $string1 ]
then
echo "It is not null"
else
echo "This is null"
fi
一个未经初始化的变量string1,第一段测试,输出结果:It is not null
第二段测试,输出结果:This is null
第三段测试,输出结果:It is not null
从结果我们看出,未经初始化的变量,在测试过程中,如果未使用"$string1"形式引用后判断,那么结果可能是错误的。