shell 变量的默认值

Posted 翔云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于shell 变量的默认值的主要内容,如果未能解决你的问题,请参考以下文章

shell 变量的默认值

shell 给未定义的变量设定默认值 ${parameter:-word}

玩转Bash变量

Linux拓展之Shell默认变量值

Shell 字符串操作符实例

玩转Bash变量