shell 变量的默认值

默认值表达式1

${a-defaultvalue}

a如果没有定义,则表达式返回默认值,否则返回a的值;

demo1

a=""

ret1=${a-"/usr/local"}
echo "ret1:" $ret1

output:

ret1:

demo2

ret1=${a-"/usr/local"}
echo "ret1:" $ret1

output:

ret1:/usr/local

默认值表达式2

${a:-defaultvalue}

a没有定义或者为空字符串,则表达式返回默认值,否则返回a的值;

demo1

a=""

ret1=${a:-"/usr/local"}
echo "ret1:" $ret1

output:

ret1:/usr/local

demo2

ret1=${a:-"/usr/local"}
echo "ret1:" $ret1

output:

ret1:/usr/local

猜你喜欢

转载自www.cnblogs.com/lanyangsh/p/10055900.html