Gra*_*ent 5 bash eval curly-braces
我很难理解我的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
Run Code Online (Sandbox Code Playgroud)ric*_*ici 10
${var:-default}
手段 $var if $var is defined and otherwise "default"
${var:+value}
手段 if $var is defined use "value"; otherwise nothing
第二个可能看起来有点奇怪,但你的代码片段显示了一个典型的用法:
${debian_chroot:+($debian_chroot)}
Run Code Online (Sandbox Code Playgroud)
这意味着"如果定义$ debian_chroot,则将其插入括号中."
上面,"已定义"表示"设置为某些非空值".Unix shell通常不区分未设置的变量和设置为空字符串的变量,但是如果使用未设置的变量,则可以告诉bash引发错误条件.(你这样做set -u
.)在这种情况下,如果debian_chroot
从未设置过,$debian_chroot
将导致错误,而如果已设置${debian_chroot:-}
则使用$debian_chroot
,否则为空字符串.
归档时间: |
|
查看次数: |
2138 次 |
最近记录: |