“export PS1=”用于自定义 shell 提示符

“export PS1=”用于自定义 shell 提示符

我想自定义我的 shell 提示符以包含时间。所以我就这么做了export PS1='\t\w\$'

我的提示现在看起来像18:57:37~$。我不知道如何在它前面加上username@hostname

\t另外,我不知道如何改变、\w等等每个参数的颜色。

经过所有测试后,我该如何将其恢复为默认值?

最后,导出行去哪儿了?我查看了~/.profile,但没有找到这一行export PS1='\t\w\$'

答案1

PS1在您的 中设置~/.bashrc。此文件包含将应用于每个交互式 shell 的设置。交互式 Bash shell 是您在 Ubuntu 中打开终端时获得的,除非您为用户设置了不同的默认 shell。

在交互式 shell 中,我们需要一个提示符,如果提示符能给我们提供一些有用的信息,比如当前工作目录、当前用户和主机名,就像 Ubuntu 一样,那就太好了PS1

以下是我的系统PS1默认版本中设置的行,.bashrc/etc/skel/.bashrc

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
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
unset color_prompt force_color_prompt

从中可以看出和的PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '转义码分别是和usernamehostname\u\h

zanna@toaster:~$ PS1="\u@\h"
zanna@toaster

如果要添加时间和当前工作目录:

zanna@toasterPS1="\u@\h \t \w "
zanna@toaster 10:43:32 ~ 

要获取颜色,您需要使用颜色转义序列。您可以在 color_prompt 赋值中看到一些.bashrc

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

例如,\033[01;34m是蓝色:

PS1 分配

哎呀!现在后面的文本也是蓝色的……最好将其改回白色:

重新分配颜色的 PS1

我们应该用转义的方括号括住颜色分配,否则 Bash 会认为它们正在打印提示符的字符并使用它们来计算其大小。当您尝试与历史记录交互时,这会产生奇怪的效果,因此这里是更正后的版本:

PS1="\[\033[01;34m\]\u@\h \t \w \[\033[00m\]"

播放完毕后,您可以通过以下方式将提示恢复为默认提示:关闭终端并打开一个新终端;) 或者运行

source ~/.bashrc

我的 PS1 恢复正常

PS1使用 中的代码像这样设置.bashrc,取消注释#force_color_prompt=yes并更改颜色代码。在这里您可以看到我为设置它而更改的行:

$ diff .bashrc /etc/skel/.bashrc 
46c46
< force_color_prompt=yes
---
> #force_color_prompt=yes
60c60
<     PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;35m\]\w\$\[\033[00m\] '
---
>     PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

(此后还有更多行更改,但它们并不相关)

您可以做同样的事情,但在某行\t中添加一个color_prompt,例如

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h \[\033[01;36m\]\t \[\033[00m\]:\[\033[01;35m\]\w\$\[\033[00m\] '

有关颜色和更多内容的 ANSI 转义码列表,请参阅本指南用于自定义提示


我忘了回答你问题的最后一部分。

最后,export 行去哪儿了?我查看了~/.profile,但没有找到 export 行PS1='\t\w\$'

我不确定您是否预期运行export VAR=val会导致您的~/.profile被自动修改。该export命令永远不会这样做。导出变量只会将其传递到从当前 shell 运行的命令环境中。当您退出 shell(并且其所有子进程都已退出)时,您从 shell 导出的任何东西都会消失。

如果要永久设置环境变量,通常需要明确添加~/.profile。您可能用来安装软件的某些脚本可能会修改您的~/.profile或其他 shell 配置文件。

PS1不需要导出到环境中。在我的回答开始时,我说在交互式 shell 中我们需要一个提示符,我的意思是仅有的交互式 shell 需要提示符(因为提示符可以帮助用户与 shell 交互)。其他命令都不需要PS1

您可能认为PS1将 传递给当前 shell 的任何子 shell 可能会很有用。当您通过运行 在 shell 中启动交互式 shell 时bash,新 shell 将不会继承调用 shell 的 shell 变量;只会继承其环境变量。因此,要将变量传递给子 shell,我们应该将export它们传递给子 shell。

但是导出PS1通常*无法将其值传递给子 shell,因为它被 shell 的配置文件重置了,/etc/bash.bashrc并且~/.bashrc。因此,关闭终端(如我之前建议的那样)是没有必要的;即使运行bash也会将您的提示返回到其通常形式:

zanna@toaster:~$ export PS1='\t -> '
22:43:54 -> bash
zanna@toaster:~$ 

(如果你使用exit这个 shell,编辑后的提示符就会返回)

* 我说通常,是因为尽管非交互式 shell总是unset PS1,如果设置了 ,交互式 Bash shell 将保留 的值PS1。这并不明显,因为如上例所示,它通常会被配置文件重置。我们可以通过更改PS1然后启动一个不读取配置文件的新 shell 来发现它:

zanna@toaster:~$ export PS1='\t -> '
22:55:04 -> bash --norc
22:55:09 -> 

因此,总结一下,中没有 的export行,因为不是环境变量,也没有必要成为环境变量,因为只有交互式 shell 才需要它,而由于交互式 shell 需要,所以它被设置在 中,因为除非被告知不要,否则所有交互式 Bash shell ,所以不需要被子 shell 继承(但如果你确实想要将中的某个值以外的某个值传递给子 shell,你可以通过阻止该 shell 获取 来实现)。PS1~/.profilePS1PS1~/.bashrcsource ~/.bashrc~/.bashrcexport PS1exportPS1.bashrc.bashrc


感谢 Eliah Kagan 的帮助,我得以扩展我的答案,他解释了 Bash 如何处理 PS1在聊天中更详细地这个答案

相关内容