使用-z
或-n
对一个变量判空时,需要注意若直接使用[ -n ${ARG} ]
这种形式,若${ARG}
中有空格将会报错,
#!/bin/bash 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://www.cnblogs.com/faberbeta/p/linux-shell025.html
时间: 2024-11-05 21:34:52