linux – shell:当var未设置或为null时,${var: – }的目的是什
发布时间:2020-12-13 22:59:35 所属栏目:Linux 来源:网络整理
导读:在我的 Linux Mint 17.2 /etc/bash.bashrc 中,我看到以下内容: # 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)f
在我的
Linux Mint 17.2
/etc/bash.bashrc 中,我看到以下内容:
# 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? Bash的Shell Parameter Expansion说:
这里,“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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |