Shell判断输入变量或者参数是否为空

Shell Judge Empty

Posted by BlueFat on Saturday, August 19, 2017

判断变量为空

if  [ ! -n "$word" ] ;then
    echo "you have not input a word"
else
    echo "the word you input is $word"
fi

直接判断

if  [ ! "$word" ] ;then
    echo "you have not input a word"
else
    echo "the word you input is $word"
fi

使用test判断

if test -z "$word" ;then
    echo "you have not input a word"
else
    echo "the word you input is $word"

使用"“判断

if [ "" = "$word" ] ;then
    echo "you have not input a word"
else
    echo "the word you input is $word"
fi