Bash变量默认值(缺省值)随记
来自三线的随记
Form 1
${a-defaultvalue}
a如果没有定义,则表达式返回默认值,否则返回a的值;
demo
sanxian@sanxian-pc $ unset test sanxian@sanxian-pc $ echo ${test-"strstr"} strstr sanxian@sanxian-pc $ test="" sanxian@sanxian-pc $ echo ${test-"strstr"} sanxian@sanxian-pc $
Form 2
${a:-defaultvalue}
a没有定义或者为空字符串,则表达式返回默认值,否则返回a的值;
demo
sanxian@sanxian-pc $ test=1123 sanxian@sanxian-pc $ echo ${test:-"strstr"} 1123 sanxian@sanxian-pc $ test="" sanxian@sanxian-pc $ echo ${test:-"strstr"} strstr sanxian@sanxian-pc $ unset test sanxian@sanxian-pc $ echo ${test:-"strstr"} strstr