修改bash中PS1命令提示符的颜色

1. 对比Ubuntu与CentOS的命令提示符
在Ubuntu-20.04-LTS版本中,PS1的颜色相关配置在~/.bashrc
这个文件中直接给出了,用户可以通过修改对应部分的取值达到修改命令提示符显示颜色的目的。颜色设置部分脚本内容,具体如下所示:
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# 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\]\$ '
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\$ '
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\W\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
#PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \W\a\]$PS1"
;;
*)
;;
esac
上述配置的显示效果如下图所示:
但是在RHEL/CentOS-7.x系统(不限于7.x系统,RHEL/CentOS系列以及衍生版系统的~/.bashrc
配置文件中都不包含PS1变量的定义)则并没有相关定义。这个文件的内容很简单,并不像Ubuntu系统中的那样丰富。具体如下所示:
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
上述是CentOS-7.6发行版中的~/.bashrc
文件的全部内容。所以CentOS的命令提示符只是默认的黑色字体显示。
2. CentOS命令提示符颜色设置
要想在CentOS中实现命令提示符的颜色显示,需要在bash中设置环境变量PS1,具体如下所示:
[root@c7u6s1:~]# export PS1="[\[\e[1;32m\]\u\[\e[m\]\[\e[1;36m\]@\h:\[\e[m\]\[\e[1;33m\]\W\[\e[m\]]\\$ "
[root@c7u6s1:~]#
设置之后的效果如下图所示:
上述执行方式是在这个terminal中有效,如果再新开一个terminal,那么就仍然会变为黑色字体。此时可以将上述的语句写入到
~/.bashrc
这个配置文件中,也可以在/etc/profile.d/
目录中创建一个.sh
为后缀的文件,将上述语句粘贴在其中即可。分别介绍如下:
-
写入到
~/.bashrc
文件中通过文本编辑器修改上述文件,加入
export PS1=
这条命令之后的结果如下所示:[root@c7u6s1:mezzanine]# vim ~/.bashrc [root@c7u6s1:mezzanine]# cat ~/.bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi export PS1="[\[\e[1;32m\]\u\[\e[m\]\[\e[1;36m\]@\h:\[\e[m\]\[\e[1;33m\]\W\[\e[m\]]\\$ " [root@c7u6s1:mezzanine]#
上述
export PS1=
这一行即为添加的修改命令提示符颜色的命令。 -
写入到
/etc/profile.d/color_ps1.sh
文件中创建上述文件,然后将
export PS1=
这条语句加入到其中,具体如下所示:[root@c7u6s1:mezzanine]# vim /etc/profile.d/color_ps1.sh [root@c7u6s1:mezzanine]# cat /etc/profile.d/color_ps1.sh export PS1="[\[\e[1;32m\]\u\[\e[m\]\[\e[1;36m\]@\h:\[\e[m\]\[\e[1;33m\]\W\[\e[m\]]\\$ " [root@c7u6s1:mezzanine]#
上述即为新增的文件以及其中的内容。
上述这两种方式都是对于下次开启的新的终端有效,如果要对当前终端生效,需要借助source
命令或者.
加载上述文件的内容。具体如下所示:
[root@c7u6s1:mezzanine]# . /etc/profile.d/color_ps1.sh
[root@c7u6s1:mezzanine]# source /etc/profile.d/color_ps1.sh
[root@c7u6s1:mezzanine]# . ~/.bashrc
[root@c7u6s1:mezzanine]# source ~/.bashrc
[root@c7u6s1:mezzanine]#
上述4种形式效果是一样的,根据环境变量所在的文件选择执行即可。
3. 颜色以及相关控制字符的含义解释
在上述的export PS1=
命令中,使用了一些特殊的控制字符。其中,主要分为颜色控制部分和内容控制部分。
-
颜色控制部分有分为起始标记和结束标记,用于表示颜色控制范围。起始标记形式为
\[\e[x;y;z\]
(其中的x, y, z表示颜色控制字符,通常我习惯将x的位置作为字体格式控制字符;y的位置作为前景色控制字符;z的位置作为背景色控制字符,并不是三者必须同时出现,可以省略某个位置的控制字符,各个控制字符之间用分号分隔),用于表示该颜色控制范围的起始部分;结束标记形式为\[\e[m\]
,与前面的起始标记相匹配,表示颜色控制范围到此结束,后面的内容不再受此处的颜色控制字符影响。颜色控制部分的颜色以及字体控制字符具体如下所示:
前景色(F)代码 背景色(B)代码 颜色名称 30 40 黑色 31 41 红色 32 42 绿色 33 43 黄色 34 44 蓝色 35 45 洋红色(紫红色) 36 46 青蓝色 37 47 白色 除了上述的颜色控制字符之外,还包含如下的字符格式控制字符,具体如下所示:
字体格式控制代码 代码含义 0 恢复默认属性设置 1 设置为粗体 2 设置一半亮度(模拟彩色显示器) 4 设置下划线 5 设置闪烁 7 设置反向图形 22 设置为一般密度 24 关闭下划线 25 关闭闪烁 27 关闭反向图像 38 在缺省的前景色上设置下划线 39 在缺省的前景色上关闭下划线 49 背景色设置为默认黑色 -
内容部分以转义字符的形式存在,比如上面的
\u
(表示当前登录的用户名),\h
(表示当前登录的主机名),\W
(表示当前的工作目录的名称,非完整路径名称,最后一级路径名)等等。具体的内容部分支持的字符如下所示:
\d
:代表日期,格式为weekday month date,例如:“Mon Aug 1”\H
:完整的主机名称\h
:仅取主机的第一个名字\t
:显示时间为24小时格式,如:HH:MM:SS\T
:显示时间为12小时格式\A
:显示时间为24小时格式:HH:MM\u
:当前用户的账号名称\v
:BASH的版本信息\w
:完整的工作目录名称\W
:利用basename取得工作目录名称,所以只会列出最后一个目录\#
:下达的第几个命令\\$
:提示字符,如果是root时,提示符为:# ,普通用户则为:$\n
:表示换行
4. 使用tput
命令修改终端命令提示符的样式
除了使用上述的方式修改终端命令提示符的样式之外,还可以使用tput
命令修改终端命令提示符的样式。比如使用tput
命令将终端命令提示符的样式修改为红色,具体如下所示:
$ export PS1="\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]
修改后的样式如下所示:
如果不想在上述的命令中将颜色使用硬编码的形式指定,可以使用下面的变量形式,具体如下所示:
_GREEN=$(tput setaf 2)
_BLUE=$(tput setaf 4)
_RED=$(tput setaf 1)
_RESET=$(tput sgr0)
_BOLD=$(tput bold)
export PS1="${_GREEN}\h${_BLUE}@${_RED}\u${_RESET} ${_BOLD}\$ ${_RESET}"
上述命令的执行效果如下所示:
4.1. 几条方便的tput
命令
tput bold
:粗体效果tput rev
:显示反转的颜色(Display inverse color)tput sgr0
:复位默认样式tput setaf {CODE}
:设置前景色,{CODE}用于指定具体的颜色,参见下面的颜色代码tput setab {CODE}
:设备背景色,{CODE}用于指定具体的颜色,参见下面的颜色代码
4.2. tput
命令的颜色代码表
关于{CODE}部分支持的颜色代码,如下表所示:
Color {code} | Color |
---|---|
0 | Black |
1 | Red |
2 | Green |
3 | Yellow |
4 | Blue |
5 | Magenta |
6 | Cyan |
7 | White |
5. References
[1]. BASH Shell Change The Color of Shell Prompt on Linux or UNIX