# 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