Reputation: 1130
foo=${foo:-default}
bar=${bar-default}
Is there a functional difference between including the colon :
and excluding it?
Upvotes: 1
Views: 56
Reputation: 52451
See the spec:
$ myvar=
$ echo "${myvar:-default}"
default
$ echo "${myvar-default}"
$ unset myvar
$ echo "${myvar-default}"
default
With the colon, the check is for "if unset or null, use default". Without the colon, it's just "if unset, use default".
The relevant paragraph (emphasis mine):
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.
Same goes for Bash, by the way. Quoting from the manual:
When not performing substring expansion, using the form described below (e.g.,
:-
), Bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter's existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
Upvotes: 4