Test for a Bash variable being unset, using a function

后端 未结 7 1305
逝去的感伤
逝去的感伤 2020-12-07 11:37

A simple Bash variable test goes:

${varName:?    \"${varName} is not defined\"}

I\'d like to re-use this, by putting it in a function. How

7条回答
  •  隐瞒了意图╮
    2020-12-07 11:55

    Unsure if this is exactly what you want but a handy trick I use when writing a new+complex script is to use "set -o"

    set -o #will make the script bomb out when it finds an unset variable

    EG:

    $ grep '$1' chex.sh

    case "$1" in

    $ ./chex.sh

    ./chex.sh: line 111: $1: unbound variable

    $ ./chex.sh foo

    incorrect/no options passed.. exiting

提交回复
热议问题