What does '-' (dash) mean after variable names?
if [ -n "${BASH-}" -o -n "${ZSH_VERSION-}" ] ; then hash -r 2>/dev/null fi Where can I find a link to this? Thanks.
The variables inside ${...} are called "Parameter Extension".
Look for this term in the online manual or this manual (line 792).
The form ${var-} is similar in form to ${var:-} . The difference is explained just one line before the extension :- (line 810):
... bash checks for an unset or null parameter. Omitting the colon results in a check only for a parameter that is not set.
Thus, this form only checks when the variable is not set ( and not zero), and replaces the entire extension ${...} with the value after - , which in this case is zero .
Therefore, ${var-} becomes:
- The value of var when var has a value (and not zero).
- Also the value of var (colon: absent!), When var is null:
'', thus: also null. - The value after - (in this case, zero
'') if the variable is not set.
All this is only in fact:
- Expand to
''when var is either not installed or is zero. - Expand to the value of the var variable (if it matters).
Therefore, the extension does not change anything in the value of var or in the extension, it simply avoids a possible error if the nounset option is installed in the shell.
This code will stop in both cases of using $var :
#!/bin/bash set -u unset var echo "variable $var" [[ $var ]] && echo "var set" However, this code will work without errors:
#!/bin/bash set -u unset var echo "variable ${var-}" [[ ${var-} ]] && echo "var set" This is a bash parameter extension that is used to check if a variable is set.
Explanation
When you use ${ZSH_VERSION-WORD} instead of $ZSH_VERSION in a bash script, bash will execute additional logic
if $ZSH_VERSION is set then simply use the value of $ZSH_VERSION as per normal elseif $ZSH_VERSION is NOT set then use value of WORD - which isnt provided in your case - so null is used
References
The extension of the base parameter is described in the man bash docs (line 939 of the man bash page).
see: POSIX
also see this SO answer
Under normal conditions, ${FOO-} behaves exactly like ${FOO} .
However, with set -u , unlocking undefined variables becomes a default error.
So ${FOO} may be a mistake, but ${FOO-} will never be.