Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ${VARIABLE+set} mean?

Tags:

bash

I'm looking through someone else's code and am not sure what this means. It is either a variable call VARIABLE+set which is a strange variable name since is has a +, or is is evaluated and is hard to Google because it has ${} in it ;)

like image 291
Rick Smith Avatar asked Nov 09 '11 01:11

Rick Smith


1 Answers

It took be some time, but I found a link explaining what this does. It is a form of bash parameter-substitution that will evaluate to "set" if $VARIABLE has been set and null otherwise. This allows you to check if a variable is set by doing the following:

if [ -z "${VARIABLE+set}" ] ; then
    echo "VARIABLE is not set"
fi

It is also interesting to note that ${VARIABLE+set} can just as easily be ${VARIABLE+anything}. The only reason for using +set is because it is slightly more self-documenting (although not enough to keep me from asking this question).

like image 181
Rick Smith Avatar answered Sep 22 '22 03:09

Rick Smith