linux shell null,linux – shell:当var未设置或为null时,${var: – }的目的是什么?...

# set variable identifying the chroot you work in (used in the prompt below)

if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then

debian_chroot=$(cat /etc/debian_chroot)

fi

这是对令牌debian_chroot的第一次引用.

为什么这段代码使用${debian_chroot: – }而不只是$debian_chroot?

${parameter:-word}

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

这里,“word”为null,为什么还要将null替换为null?

解决方法:

语法${debian_chroot: – }阻止shell在使用set -u运行时退出(使用未定义变量时崩溃)并且此时未设置debian_chroot.

您不希望普通的交互式shell设置-u(它会很容易崩溃),但它在脚本中非常有用.

看到这个:

bash -c 'set -u; [ -z $a ]; echo ok' # error

bash -c 'set -u; a=; [ -z $a ]; echo ok' # ok

bash -c 'set -u; [ -z ${a:-} ]; echo ok' # ok

bash -c 'set -u; a=; [ -z ${a:-} ]; echo ok' # ok

标签:bash,shell,linux

来源: https://codeday.me/bug/20190628/1310358.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值