How to find whether or not a variable is empty in Bash

后端 未结 10 1097
深忆病人
深忆病人 2020-12-02 04:17

How can I check if a variable is empty in Bash?

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 05:07

    This will return true if a variable is unset or set to the empty string ("").

    if [ -z "$MyVar" ]
    then
       echo "The variable MyVar has nothing in it."
    elif ! [ -z "$MyVar" ]
    then
       echo "The variable MyVar has something in it."
    fi
    

提交回复
热议问题