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

后端 未结 7 2025
傲寒
傲寒 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:00

    This is what I've found works best for me, taking inspiration from the other answers:

    if [ -z "${varname-}" ]; then
      ...
      varname=$(...)
    fi
    

提交回复
热议问题