如何在 centos 上自定义 bash 提示?

T. *_*nes 6 bash prompt

我已经更改了/etc/bashrc文件。

我注释掉了之前的定义并插入了我自己的定义:

PS1="--bash: (\u@\h:\w) $"
Run Code Online (Sandbox Code Playgroud)

这没有任何影响。当我在命令提示符下进行此更改时,效果很好,但不是永久性的。

Gil*_*il' 8

/etc/bashrc是系统设置。更改提示的通常位置是每个用户的文件~/.bashrc. 您可能已经在那里设置了覆盖系统默认值的设置。将您的提示字符串放入~/.bashrc.

此外,bash 在处理初始化文件方面有一个怪癖。在/etc/bashrc~/.bashrc文件是只读的未登录shell的bash的互动情况。如果您运行登录 shell,bash 只会读取/etc/profile, and ~/.bash_profile(或者~/.profile如果它不存在)。如果登录 shell 是交互式的,您需要明确地.bashrc从您的源中获取.bash_profile

# Work around a well-known bash design bug: bash does not read .bashrc if
# it is invoked as a login shell.
case "$-" in *i*) if test -r ~/.bashrc; then . ~/.bashrc; fi;; esac
Run Code Online (Sandbox Code Playgroud)