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

我想自定义shell提示符以包含时间。 所以,我确实export PS1='\t\w\$'

我的提示现在看起来像18:57:37~$ 。 我不知道如何用username@hostname作为前缀。

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

在所有测试之后,我如何将其设置回默认值?

最后,出口线在哪里? 我查看了~/.profile ,但没有行export PS1='\t\w\$'

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

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

以下是在我的系统的默认版本.bashrc设置PS1的行,/ /etc/skel/.bashrc .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 ~ 

要获得颜色,您需要使用颜色转义序列。 您可以在.bashrc中的color_prompt分配中看到一些内容

 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恢复正常

我使用.bashrc的代码设置我的PS1 ,取消注释#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\]\$ ' 

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

例如,您可以执行相同操作,但在某处将color_prompt行添加\t

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

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


我忽略了回答你问题的最后部分。

最后,出口线在哪里? 我看了~/.profile ,但没有行导出PS1='\t\w\$'

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

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

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

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

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

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

(如果exit此shell,编辑的提示会返回)

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

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

因此,总而言之, ~/.profile没有PS1 export行,因为PS1不是环境变量,并且没有业务是一个,因为只有交互式shell需要它,并且由于交互式shell需要PS1 ,它是在~/.bashrc设置,因为除非告诉不要,所有交互式Bash shell都source ~/.bashrc ,所以~/.bashrc不需要export PS1PS1 shellinheritance(但是如果你真的想export PS1某些值除了.bashrc中的一个值到子shell之外,你可以通过阻止shell来源.bashrc


感谢Eliah Kagan,我已经能够扩展我的答案,他解释了Bash如何在聊天中对待PS1 ,并在此答案中更详细地说明了这一点 。