使用-z
或-n
对一个变量判空时,需要注意若直接使用[ -n ${ARG} ]
这种形式,若${ARG}
中有空格将会报错,
ARG="sd dd"
if [ -n ${ARG} ]; then
echo 'ARG:' ${ARG}
else
echo 'ARG is empty.'
fi
输出:
line 27: [: sd: binary operator expected
ARG is empty.
显然不对
解决方法,使用[[ -n ${ARG} ]]
或[ -n "${ARG}" ]
参考:
https://digitalvectorz.wordpress.com/2009/12/10/bashshell-programming-binary-operator-expected/