翔云

Just try, don't shy. 最新文章请点击
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

shell 变量的默认值

Posted on 2018-12-02 22:34  翔云123456  阅读(482)  评论(0编辑  收藏  举报

默认值表达式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