POSIX 默认字符串的语法差异(冒号)?

POSIX syntax difference (colon) in defaulting a string?

foo=${foo:-default}

bar=${bar-default}

包含冒号 : 和排除冒号在功能上有区别吗?

参见the spec

$ myvar=
$ echo "${myvar:-default}"
default
$ echo "${myvar-default}"

$ unset myvar
$ echo "${myvar-default}"
default

使用冒号,检查 "if unset or null, use default"。没有冒号,它只是 "if unset, use default".

相关段落(强调我的):

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.

顺便说一句,Bash 也是如此。引用自 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.