Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash set -u allow single unbound variable

Tags:

variables

bash

With bash, you can allow set -e to fail a specific command like:

someCommandThatProbablyFails || true

Is there anything for set -u to allow a specific variable to be unbound?

Specifically I'm doing something like:

if [ -n "${1}" ]; then
    SOMEVAR="${1}"
else
    SOME_STUFF_TO_DETERMINE_SOMEVAR
fi

In this case, it's "${1}" that triggers the unbound variable error if nothing was passed into the script from the command line.

like image 711
SnakeDoc Avatar asked Jan 20 '15 06:01

SnakeDoc


1 Answers

You can use an empty string as the default for an unset variable like this:

${POSSIBLY_UNSET_VARIABLE-}
like image 158
John Zwinck Avatar answered Oct 04 '22 14:10

John Zwinck