Variables inside a ${...}
are called ? Parameter Expansion ?.
Search for that term in the online manual, or the actual manual (line 792).
The ${var-}
form is similar in form to ${var:-}
. The difference is explained just one line before the :-
expansion (line 810):
... bash tests for a parameter that is unset or null. Omitting the colon
results in a test only for a parameter that is unset.
Thus, this form is testing only when a variable is unset (and not null), and replaces the whole expansion ${...}
for the value after the -
, which in this case is null.
Therefore, the ${var-}
becomes:
- The value of var when var has a value (and not null).
- Also the value of var (the colon : is missing!) when var is null:
''
, thus: also null.
- The value after the - (in this case, null
''
) if var is unset.
All that is just really:
- Expand to
''
when var is either unset or null.
- Expand to the value of the var (when var has a value).
Therefore, the expansion changes nothing about the value of var, nor it's expansion, just avoids a possible error if the shell has the option nounset
set.
This code will stop on both uses of $var
:
#!/bin/bash
set -u
unset var
echo "variable $var"
[[ $var ]] && echo "var set"
However this code will run without error:
#!/bin/bash
set -u
unset var
echo "variable ${var-}"
[[ ${var-} ]] && echo "var set"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…