Smi*_*ile 4 command-line bash prompt bashrc ps1
我想自定义我的 shell 提示以包含时间。所以,我做到了export PS1='\t\w\$'。
我的提示现在看起来像18:57:37~$. 我不知道如何在它前面加上username@hostname.
另外,我不知道如何改变颜色的每个参数为\t,\w,等等。
在所有测试之后,我如何将其设置回默认值?
最后,出口线去哪儿了?我看了看~/.profile,但没有线export PS1='\t\w\$'。
PS1设置在您的~/.bashrc. 此文件包含将应用于每个交互式 shell 的设置。除非您为用户设置了不同的默认 shell,否则在 Ubuntu 中打开终端时将获得交互式 Bash shell。
在交互式 shell 中,我们需要一个提示,如果提示为我们提供了一些有用的信息,比如当前工作目录、当前用户和主机名,就像 UbuntuPS1那样,那就太好了。
这是在我的系统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
Run Code Online (Sandbox Code Playgroud)
从PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '您可以看到,对于转义码username和hostname是\u和\h分别
zanna@toaster:~$ PS1="\u@\h"
zanna@toaster
Run Code Online (Sandbox Code Playgroud)
如果要添加时间和当前工作目录:
zanna@toasterPS1="\u@\h \t \w "
zanna@toaster 10:43:32 ~
Run Code Online (Sandbox Code Playgroud)
要获得颜色,您需要使用颜色转义序列。您可以在 color_prompt 分配中看到一些.bashrc
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
Run Code Online (Sandbox Code Playgroud)
例如,\033[01;34m是蓝色:
哎呀!现在后面的文字也是蓝色的……最好把它改回白色:
我们应该用转义的方括号将颜色分配括起来,否则 Bash 会认为它们正在打印提示的字符并使用它们来计算其大小。当您尝试与历史记录交互时,这会产生奇怪的效果,因此这是更正后的版本:
PS1="\[\033[01;34m\]\u@\h \t \w \[\033[00m\]"
Run Code Online (Sandbox Code Playgroud)
播放完毕后,您可以通过关闭终端并打开一个新终端来将提示恢复为默认值;) 或运行
source ~/.bashrc
Run Code Online (Sandbox Code Playgroud)
我PS1使用已经在 中的代码进行了这样的设置.bashrc,取消注释#force_color_prompt=yes并更改颜色代码。在这里你可以看到我已经改变了设置它的行:
zanna@toaster:~$ PS1="\u@\h"
zanna@toaster
Run Code Online (Sandbox Code Playgroud)
(此后更改了更多行,但它们不相关)
你可以做同样的事情,但\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\] '
Run Code Online (Sandbox Code Playgroud)
有关颜色和更多内容的 ANSI 转义码列表,请参阅本指南以自定义提示。
我忽略了回答你问题的最后一部分。
最后,出口线去哪儿了?我看了看
~/.profile,但没有出口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它们。
但是导出PS1通常*无法将其值传递给子 shell,因为它被 shell 的配置文件重置,/etc/bash.bashrc并且~/.bashrc. 因此,没有必要关闭终端(正如我之前建议的那样);即使运行bash也会将您的提示恢复为通常的形式:
zanna@toaster:~$ export PS1='\t -> '
22:43:54 -> bash
zanna@toaster:~$
Run Code Online (Sandbox Code Playgroud)
(如果你是exit这个 shell,编辑过的提示会回来)
* 我通常这么说,因为尽管非交互式 shell总是unset PS1,但交互式 Bash shell 会保留设置的值PS1。这并不明显,因为如前面的示例所示,它通常会被配置文件重置。我们可以通过更改PS1然后启动一个不读取我们的配置文件的新 shell来发现它:
zanna@toaster:~$ export PS1='\t -> '
22:55:04 -> bash --norc
22:55:09 ->
Run Code Online (Sandbox Code Playgroud)
所以,总而言之,没有export一行 for PS1in~/.profile因为PS1它不是环境变量并且没有业务是一个,因为只有交互式 shell 需要它,并且由于交互式 shell 需要PS1,它被设置 in ~/.bashrc,因为除非被告知不要,所有交互式 Bash shell source ~/.bashrc,因此~/.bashrc不需要被子 shellexport PS1继承(但如果您确实想要export某个值而PS1不是.bashrc子 shell 中的值,则可以通过阻止该 shell 采购来实现此目的.bashrc)。
感谢 Eliah Kagan,我能够扩展我的回答,他解释了 Bash 如何在聊天中处理 PS1 ,并在此回答中更详细地解释了。