In Bash, how do I test if a variable is defined in “-u” mode

后端 未结 7 1995
傲寒
傲寒 2020-12-23 09:14

I just discovered set -u in bash and it helped me find several previously unseen bugs. But I also have a scenario where I need to test if a variable is defined

7条回答
  •  借酒劲吻你
    2020-12-23 10:05

    In the beginning of your script, you could define your variables with an empty value

    variable_undefined=""
    

    Then

    if [ "${variable_undefined}" == "" ]; then
        variable="$(...)"
    fi
    

提交回复
热议问题