我很难理解我的 ubuntu 的 .bashrc
中的内容,如下所示。
这是我不明白的地方:
大括号和
:
之后使用的-
/+
符号的用途是什么? (例如:${debian_chroot:-} 和 ${debian_chroot:+($debian_chroot)})eval
命令。以下代码片段的工作原理。
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then debian_chroot=$(cat /etc/debian_chroot) fi if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi
最佳答案
${var:-default}
表示 $var 如果定义了 $var 否则为“default”
${var:+value}
表示如果 $var 被定义使用“值”;否则什么都没有
第二个可能看起来有点奇怪,但您的代码片段显示了一个典型的用法:
${debian_chroot:+($debian_chroot)}
这意味着“如果定义了 $debian_chroot,则将其插入括号内。”
上面,“已定义”的意思是“设置为某个非空值”。 Unix shell 通常不区分未设置的变量和设置为空字符串的变量,但是可以告诉 bash 在使用未设置的变量时引发错误条件。 (您可以使用 set -u
来做到这一点。)在这种情况下,如果从未设置过 debian_chroot
,$debian_chroot
将导致错误,而${debian_chroot:-}
将使用 $debian_chroot
如果已设置,否则为空字符串。
关于bash - 理解这个 .bashrc 脚本(大括号、eval、...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16410740/