The meaning of ${BASH-no}
is documented in §2.6.2, Parameter Expansion, of the Single Unix Specification:
${parameter:-[word]}
Use Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be substituted; otherwise, the value of parameter shall be substituted.
The meaning when the colon is omitted (as in your example) is described slightly later:
In the parameter expansions shown previously, use of the in the format shall result in a test for a parameter that is unset or null; omission of the shall result in a test for a parameter that is only unset.
Then there is a table, which may be easier to understand. Here are the relevant rows:
|
parameter Set and Not Null |
parameter Set but Null |
parameter Unset |
${parameter:-word} |
substitute parameter |
substitute word |
substitute word |
${parameter-word} |
substitute parameter |
substitute null |
substitute word |
Here is a reliable, portable way to check whether a variable is not set at all. Note that I am using a +
modifier instead of a -
modifier in the parameter expansion:
if [ "${BASH+set}" = "" ]; then
echo 'BASH not set at all'
else
echo 'BASH is set, perhaps to the empty string'
fi
The expansion of "${BASH+set}"
can only be ""
if BASH
is entirely unset. If BASH
is set, even to the empty string, then "${BASH+set}"
expands to "set"
instead.