如何在bash提示中更改颜色
我按照这里的说明和那里提供的howto链接 。 然而,似乎没有任何地方可以说我如何改变不同部分的颜色,因为目前我将其设置为:
PS1='${debian_chroot:+($debian_chroot)}[\t] \u@\h:\w\$ '
但是文字仍然是白色的,我怎么可能把时间绿色的数字,我的用户名和计算机名称淡蓝色? 我正在运行Ubuntu GNOME 15.04。
首先,打开〜/ .bashrc文件并启用颜色提示:
nano ~/.bashrc
取消注释然后行#force_color_prompt=yes
然后, if [ "$color_prompt" = yes ]; then
直接在线下更改PS1线if [ "$color_prompt" = yes ]; then
if [ "$color_prompt" = yes ]; then
至:
PS1='${debian_chroot:+($debian_chroot)}\[\033[00;32m\][\t] \[\033[01;34m\]\u@\h:\w\$ '
如您所见,01:34m为浅蓝色,00:32m为绿色。 浅绿色为01:32m而不是00:32m。
按CTRL + o ,然后按ENTER以保存文件。 按CTRL + x退出nano。
然后,运行以下命令以通过“获取”bashrc文件来应用更改:
. ~/.bashrc
现在,更改应适用于您用户下的每个新打开的终端。
点击这里获取更多信息
这是我和我的一个朋友一直在做的事情。 当您是普通用户时,它会给出灰色虚线,然后如果您以root用户身份运行命令,则虚线和文本都会变为红色。
在.bashrc
文件中,将以下代码添加到文件的底部:
if [ -f "$HOME/.bash_ps1" ]; then . "$HOME/.bash_ps1" fi
编辑:另外,也将它添加到/root/.bashrc
的底部。 这是为了通过发出命令sudo su -
切换到root
用户。 (编辑的其余部分在代码下面继续)
然后将此代码的其余部分复制并粘贴到名为/home/
的新文件中
# Fill with minuses # (this is recalculated every time the prompt is shown in function prompt_command): fill="--- " reset_style='\[\033[00m\]' # determine if root or not a=$(id|awk -F\( '{print $1}') if [ "$a" = "uid=0" ] then # for root status_style=$reset_style'\[\033[1;31m\]' # bold red; use 0;37m for lighter color command_style=$reset_style'\[\033[1;31m\]' # bold red else # for other users status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color command_style=$reset_style'\[\033[1;29m\]' # bold black fi prompt_style=$reset_style # Prompt variable: PS1="$status_style"'$fill $(date +"%m/%d/%y ")\t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\u@\h:\w\$'"$command_style " # Reset color for command output # (this one is invoked every time before a command is executed): trap 'echo -ne "\033[00m"' DEBUG function prompt_command { # create a $fill of all screen width minus the time string and a space: let fillsize=${COLUMNS}-18 fill="" while [ "$fillsize" -gt "0" ] do fill="-${fill}" # fill with underscores to work on let fillsize=${fillsize}-1 done # If this is an xterm set the title to user@host:dir case "$TERM" in xterm*|rxvt*) bname=$(basename "${PWD/$HOME/~}") echo -ne "\033]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007" ;; *) ;; esac } PROMPT_COMMAND=prompt_command
编辑(第2部分):现在创建/root
文件夹中.bash_ps1
的链接
sudo -s cd /root ln -s /home//.bash_ps1
您可以更改上述任何代码以满足您的需求。 这是我实际在工作中使用的一个,因此我知道我是否以root
用户身份输入命令,这可能有潜在危险。 此外,时间戳显示在您键入的每一行的正上方,如果您向后滚动以查看键入该命令的时间,则会更容易一些。
希望这有帮助!