在shell中,可以使用以下方式来判断一个变量是否为空:

  1. 使用if语句
if [ -z "$variable" ]; then
    echo "Variable is empty"
else
    echo "Variable is not empty"
fi
  1. 使用test命令
if test -z "$variable"; then
    echo "Variable is empty"
else
    echo "Variable is not empty"
fi
  1. 使用双括号
if [[ -z "$variable" ]]; then
    echo "Variable is empty"
else
    echo "Variable is not empty"
fi

其中,-z表示判断字符串长度是否为0,如果是则表示变量为空。$variable表示要判断的变量名。

shell判断变量是否为空

原文地址: https://www.cveoy.top/t/topic/Elb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录