就我的测试而言,它显然是一样的.@H_301_2@
有线索吗?@H_301_2@
$uno=1 $if [ -n "${uno}" ]; then echo yay\! ; fi yay! $if [ -n "${uno-}" ]; then echo yay\! ; fi yay!
如果uno未设置,我们得到 – 后面的字符串:@H_301_2@
$unset uno $echo ${uno-something} something
如果uno只是空字符串,则返回uno的值:@H_301_2@
$uno="" $echo ${uno-something} $
如果uno具有非空值,当然,则返回该值:@H_301_2@
$uno=Yes $echo ${uno-something} Yes
为什么要使用${variable-}?@H_301_2@
当脚本的正确操作很重要时,脚本编写者通常使用set -u,它会在使用unset变量时生成错误消息.例如:@H_301_2@
$set -u $unset uno $echo ${uno} bash: uno: unbound variable
要处理可能要禁止此消息的特殊情况,可以使用尾随 – :@H_301_2@
$echo ${uno-} $
[信用发现OP的full code使用了-u及其对这个问题的重要性归于Benjamin W.]@H_301_2@
文档@H_301_2@
来自man bash@H_301_2@
When not performing substring expansion,using the forms documented
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.@H_301_2@${parameter:-word}
Use Default Values. If parameter is unset or null,the expansion of word is substituted. Otherwise,the value of parameter is substituted. [emphasis added]@H_301_2@