在我的.bashrc文件中,我终端提示符的定义中,除了其他内容外,我有这段代码片段:
这是做什么的,我需要它吗?
${debian_chroot:+($debian_chroot)}
这是做什么的,我需要它吗?
回答这个问题的重要部分在于从/etc/bash.bashrc
中提取的这段代码:
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
$debian_chroot
为空,并且文件/etc/debian_chroot
存在且可读,则将变量设置为文件的内容。/etc/debian_chroot
就会出现。所以这是为了更好地进行概览,在chroot环境中还是不在其中可以加以区分。/srv/nfs4/netboot/
中有另一个系统的chroot时,你可以在/srv/nfs4/netboot/etc/debian_chroot
中设置这个chroot的名称(在我的例子中,它是一个nfs4 pxe netboot驱动)。user@host:~# echo "netboot" >/srv/nfs4/netboot/etc/debian_chroot
chroot /srv/nfs4/netboot/
(netboot)user@host:~#
${var:+value}
的意思是:if $var is defined and not null; then use 'value'; else do nothing
debian_chroot
变量在/etc/bash.bashrc
文件中定义。如果存在且可读,它将获取/etc/debian_chroot
文件的内容。默认情况下,该文件不存在。
更多详细信息,请参见:
现在,为了更好地理解其中发生的事情,请在终端中执行以下操作:
radu@Radu:~$
PS1='${var:+($var)}\u@\h:\w\$ '
radu@Radu:~$ var="test"
----
|
------------------
|
V
(test)radu@Radu:~$ var=""
radu@Radu:~$ var="and so on"
(and so on)radu@Radu:~$
$debian_chroot
存在且不为空,则将${debian_chroot:+($debian_chroot)}
替换为($debian_chroot)
(即带有括号的$debian_chroot
的值)。
$debian_chroot
在/etc/bash.bashrc
中设置为/etc/debian_chroot
的内容(默认情况下该文件不存在),并且$debian_chroot
尚未具有值。
${debian_chroot:+($debian_chroot)}
通常用于定义Bash提示符,例如。PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
etc/debian_chroot
放置在chroot根文件夹中。debian_chroot
,那么这是一个方便的地方,可以通过以下方式将命令提示符显示的时间放在这里:export PROMPT_COMMAND='debian_chroot=$(date +%r)'
rick@alien:~$ export PROMPT_COMMAND='debian_chroot=$(date +%r)'
(09:14:59 PM)rick@alien:~$
while sleep 1;do tput sc;tput cup $(($(tput lines)-1)) 1;printf `date +%r`;tput rc;done &
$PS1
中使用一个更合适的变量名呢? - Adam Katz\[
和\]
(或\001
和\002
)。Debian中的默认bash提示符会自动完成这一操作,但是您的tput
命令可能会破坏它。这与我的评论无关,在我的评论中,我建议在~/.bashrc
中定义$PS1
时使用一个单独的变量。 - Adam Katzexport PS1="(\t)$PS1"
,其中\t
或\@
如bash(1)手册的PROMPTING
部分所述。请注意,这不能处理像%r
这样的strftime字符串,因此你可以选择使用export PS1="($now)$PS1" PROMPT_COMMAND='now=$(date +%r)'
,这将保留Debian chroot指示器。 - Adam Katz