怎样给bash命令行加上颜色呢?

如何让bash显示出这样的彩色效果?

colored-bash


daniel451怎么会在同一时间内回答自己的问题呢? - kva
7@kva 在Stack Exchange网络中,鼓励在发布问题的同时回答自己的问题。 - CJ Dennis
相关链接:https://askubuntu.com/questions/123268/changing-colors-for-user-host-directory-information-in-terminal-command-prompt。它解释了如何在终端命令提示符中以不同颜色显示提示的各个部分。 - FreezingFire
如果您正在使用Termux,那么您可以安装zshell,它将改变您终端的一切。您可以阅读此帖子以获取安装说明。https://www.learntermux.tech/2020/02/how-to-install-z-shell-best-theme-for-TERMUX-2020.html - Khan Saad
6个回答

打开文本编辑器,编辑~/.bashrc文件,并取消注释以下行: #force_color_prompt=yes 修改为: force_color_prompt=yes 保存后执行source ~/.bashrc命令。

1虽然并没有完全回答问题,但我更喜欢这个结果(不那么分散注意力)。 - James Hirschorn
我认为它确实回答了。为什么不呢? - To Kra
这是按照你的方法处理后的结果:2016-10-25 16:12:15.png并不完全与问题中描述的一样。 - James Hirschorn
@JamesHirschorn 只有启用了 force_color 选项,你才能使用颜色。你可以通过 PS1 变量来自定义颜色的显示效果,可以参考我在其中一个脚本中的示例 https://github.com/to-kra/dotfiles/blob/master/scripts/gitAwarePromptInstall.sh ,该脚本还启用了 git 状态显示... 你可以在用户配置文件中导出自己喜欢的 PS1,并设置漂亮的颜色和格式。谢谢! - To Kra
1force_color_prompt=yes是启用颜色的预期方式吗?对我来说,强制听起来像是一个变通方法。 - Jaakko
@Jaakko 我之前发布了这个很久以前的帖子,这是在Debian/Ubuntu上最快也是最简单的方法。我现在使用export PS1只是因为我想修改默认设置的外观。如果你不想花太多时间在export PS1上,那就按照我提到的方法来做吧。谢谢。 - To Kra
我不知道颜色是如何启用的。我只是在想,除了"force"之外,是否有其他方式可以让使用的控制台模拟器告诉.bashrc它可以处理颜色。导出PS1也感觉有点麻烦,我对默认颜色完全满意。 - Jaakko
我一点儿都不知道,我总是使用武力。在下面的回答中,被标记为绿色勾号的那个,有我提到的PS1方法。不知道还有其他什么方式可以启用它。 - To Kra
@Jaakko 我喜欢你的询问。bashrc文件声称如下:``# 如果终端有能力,取消注释以获得彩色提示符; 默认情况下关闭,以免分散用户注意力:终端窗口的焦点应该放在命令的输出上,而不是提示符上#force_color_prompt=yes``你对此有何看法? - Rémy Hosseinkhan Boucher
2希望我的评论没有让您感到不尊重,我只是想理解它的工作原理。例如,在您提到的那些行之上,有一种不同的启用颜色的方式:xterm-color|*-256color) color_prompt=yes;; 这让我觉得如果TERM变量的值正确,颜色可能会自动启用。 - Jaakko
这正是我想要的!太棒了! - C.K.

我想出了一个解决方案:
  • 在编辑器中打开~/.bashrc文件
  • 复制以下内容并添加到.bashrc文件的末尾:

    PS1='\[\033[1;36m\]\u\[\033[1;31m\]@\[\033[1;32m\]\h:\[\033[1;35m\]\w\[\033[1;31m\]\$\[\033[0m\] '
    
  • 保存文件并重新启动bashrc:

    source ~/.bashrc
    
查看以下链接以获取可用颜色和更多选项的完整列表:

完美的颜色选择! - Puck
一个在线的图形用户界面,Easy Bash PS1 Generator - Jim Fred
+1给链接到色彩图表的赞👍 :) - bitsmack
好的,这真的很性感。为了获得更多的利润,方便地为每个服务器设置不同的颜色编码。 - Hannes Schneidermayer

一个稍微“通用”的版本 - 适用于各种环境:
(取决于 terminfo)

将此插入到您的 $HOME/.bashrc 文件中:

function fgtab {
  echo "tput setf/setb - Foreground/Background table"
  for f in {0..7}; do
    for b in {0..7}; do
      echo -en "$(tput setf $f)$(tput setb $b) $f/$b "
    done
    echo -e "$(tput sgr 0)"
  done
}

# The prompt in a somewhat Terminal -type independent manner:
cname="$(tput setf 3)"
csgn="$(tput setf 4)"
chost="$(tput setf 2)"
cw="$(tput setf 6)"
crst="$(tput sgr 0)"
PS1="\[${cname}\]\u\[${csgn}\]@\[${chost}\]\h:\[${cw}\]\w\[${csgn}\]\$\[${crst}\] "

然后执行source ~/.bashrc
之后,fgtab将显示一个带有数字的颜色表。这些数字是用于tput setf ntput setb n的,其中'n'是数字,'f'代表前景色,'b'代表背景色。 tput sgr 0将重置前景色和背景色为默认值。
正如您所看到的,更改提示符使用的颜色变得非常容易(只需在$HOME/.bashrc中编辑相同的数字即可)。
如果您希望将整个提示符的背景设置为n,请在$cname中添加$(tput setb n)

更多信息请访问:http://askubuntu.com/a/396555/289138 - Hannu
更多的是直接进行数字计算。 - Hannu
man 5 terminfo 显示了 tput 可以用于什么,虽然有点技术性,但是“要小心”,但是一些猜测和测试可能会使其正常工作。 - Hannu

我在Ubuntu 20上使用Kitty/Putty时遇到了使"force-color-prompt"工作的问题。
但是请注意默认的Ubuntu 20 .bashrc文件中的以下代码: case "$TERM" in xterm-color|*-256color) color_prompt=yes;; esac
所以在Kitty中,前往:
  1. 连接
  2. 数据
然后将"终端类型字符串"从"xterm"更改为"xterm-color",就可以了!


1如果链接失效,将答案链接中的细节添加到您的回答中是很好的做法。 - gman

如果你安装的是最小化版的Ubuntu,那么你将没有默认的.bashrc文件,其中包含了颜色和其他一些设置。这是你可以使用的默认.bashrc文件。
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# 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\]\$ '
else
    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"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi