shell判断变量是否为空
在shell中,可以使用以下方式来判断一个变量是否为空:
- 使用if语句
if [ -z "$variable" ]; then
echo "Variable is empty"
else
echo "Variable is not empty"
fi
- 使用test命令
if test -z "$variable"; then
echo "Variable is empty"
else
echo "Variable is not empty"
fi
- 使用双括号
if [[ -z "$variable" ]]; then
echo "Variable is empty"
else
echo "Variable is not empty"
fi
其中,-z表示判断字符串长度是否为0,如果是则表示变量为空。$variable表示要判断的变量名。
原文地址: https://www.cveoy.top/t/topic/Elb 著作权归作者所有。请勿转载和采集!