我可以在我的PS1提示中使用什么颜色代码?

我在PS1的提示中使用了几种颜色,如

\033]01;31\] # pink
\033]00m\]   # white
\033]01;36\] # bold green
\033]02;36\] # green
\033]01;34\] # blue
\033]01;33\] # bold yellow

我在哪里可以找到我可以使用的颜色代码的列表?

我查看了https://unix.stackexchange.com/questions/74024/colorize-bash-console-color,但它没有回答我关于实际代码列表的问题。

如果有一个更可读的表格就更好了。

另请参见https://unix.stackexchange.com/a/127800/10043

解决办法

这些是[ANSI转义序列][1];那个链接是一个颜色代码的图表,但在那个维基百科页面上还有其他有趣的东西。 并非所有这些都能在(例如)普通的Linux控制台中工作。

这是不正确的。

\033]00m\] 。 # white

0将终端重置为其默认值(可能是白色)。 白色前景的实际代码是37。 另外,结尾处的转义闭合括号(])不是颜色序列的一部分(见下面最后几段关于它们在设置提示符方面的作用的解释)。

注意,一些GUI终端允许你指定一个自定义的颜色方案。这将影响输出。

有[这里有一个列表][2],它增加了7种我以前没有见过的前景和7种背景颜色,但它们似乎是有效的。

# Foreground colors
90   Dark gray  
91   Light red  
92   Light green    
93   Light yellow   
94   Light blue 
95   Light magenta  
96   Light cyan  

# Background colors
100  Dark gray  
101  Light red  
102  Light green    
103  Light yellow   
104  Light blue 
105  Light magenta  
106  Light cyan 

此外,如果你有一个256色的GUI终端(我想现在大多数都是),你可以应用这个图表中的颜色。

![xterm 256色图][3]

选择这些的ANSI顺序,使用左下角的数字,开始是38;5;代表前景,48;5;代表背景,然后是颜色编号,所以例如。

echo -e "\\033[48;5;95;38;5;214mhello world\\033[0m"

给我的是棕褐色的浅橙色(意思是,色表是大致上的近似)。

你可以看到这个图表中的颜色1,因为它们会相当容易地出现在你的终端。

#!/bin/bash

color=16;

while [ $color -lt 245 ]; do
    echo -e "$color: \\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m"
    ((color++));
done  

输出结果是不言自明的。

有些系统通过/etc/profile'中的一些shell代码将$TERM变量设置为xterm-256color',如果你在一个256色的终端上。 在其他系统中,你应该能够配置你的终端来使用这个。 这将使TUI应用程序知道有256种颜色,并允许你在~/.bashrc中添加类似的东西。

if [[ "$TERM" =~ 256color ]]; then
     PS1="MyCrazyPrompt..."
fi

注意,当你在提示符中使用颜色转义序列时,你应该用转义(``前缀)的方括号把它们括起来,像这样。

PS1="\[\033[01;32m\]MyPrompt: \[\033[0m\]"

注意,颜色序列内部的`[''没有被转义,但包围的部分被转义了。 后者的目的是向shell表明,包围的序列不计入提示符的字符长度。 如果这个计数是错误的,当你向后滚动历史记录时就会发生奇怪的事情,例如,如果它太长,最后滚动的字符串的多余长度会出现在你的提示符上,你将无法退格到它(它被忽略的方式与提示符相同)。

还要注意的是,如果你想在每次使用提示符时都包括命令的输出(而不是在设置提示符时只有一次),你应该把它设置成一个带单引号的字面字符串,例如。

PS1='\[\033[01;32m\]$(date): \[\033[0m\]'

如果你喜欢使用bash的特殊的dD{format}提示符转义,这不是问题的主题,但可以在man bashPROMPTING下找到,这不是一个好例子。 还有其他一些有用的转义,如w表示当前目录,u表示当前用户,等等。


这个图表的主要部分,颜色16-231(注意它们不是按数字顺序排列的)是一个6×6×6的RGB颜色立方体。 "颜色立方体"是指RGB颜色空间可以用一个三维数组来表示(一个轴代表红色,一个轴代表绿色,一个轴代表蓝色)。 这里的立方体中的每种颜色都可以表示为6×6×6数组中的坐标,图表中的索引是这样计算的:

评论(16)

看起来至少有一部分名单是这样的。

txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m'   # Black - Background
bakred='\e[41m'   # Red
bakgrn='\e[42m'   # Green
bakylw='\e[43m'   # Yellow
bakblu='\e[44m'   # Blue
bakpur='\e[45m'   # Purple
bakcyn='\e[46m'   # Cyan
bakwht='\e[47m'   # White
txtrst='\e[0m'    # Text Reset

根据https://wiki.archlinux.org/index.php/Color_Bash_Prompt

评论(1)

另一个脚本就像TAFKA 'goldilocks'发布的用于显示颜色的脚本,对于参考目的来说可能更实用一点。

#!/bin/bash

useage() {
  printf "\n\e[1;4mAscii Escape Code Helper Utility\e[m\n\n"
  printf " \e[1mUseage:\e[m colors.sh [-|-b|-f|-bq|-fq|-?|?] [start] [end] [step]\n\n"
  printf "The values for the first parameter may be one of the following:\n\n"
  printf " \e[1m-\e[m  Will result in the default output.\n"
  printf " \e[1m-b\e[m This will display the 8 color version of this chart.\n"
  printf " \e[1m-f\e[m This will display the 256 color version of this chart using foreground colors.\n"
  printf " \e[1m-q\e[m This will display the 256 color version of this chart without the extra text.\n"
  printf " \e[1m-bq\e[m    This will display the 8 color version of this chart without the extra text.\n"
  printf " \e[1m-fq\e[m    This will display the 256 color version of this chart using foreground colors without the extra text.\n"
  printf " \e[1m-?|?\e[m   Displays this help screen.\n"
  printf "\nThe remaining parameters are only used if the first parameter is one of: \e[1m-,-f,q,fq\e[m\n\n"
  printf " \e[1mstart\e[m  The color index to begin display at.\n"
  printf " \e[1mend\e[m    The color index to stop display at.\n"
  printf " \e[1mstart\e[m  The number of indexes to increment color by each iteration.\n\n\n"

}
verbose() {
  if [[ "$1" != "-q" && "$1" != "-fq" && "$1" != "-bq" ]]; then
    printf "\nTo control the display style use \e[1m%s\e[m where \e[1m%s\e[m is:\n" '\e[{$value}[:{$value}]m' '{$value}'
    printf "\n  0 Normal \e[1m1 Bold\e[m \e[2m2 Dim\e[m \e[3m3 ???\e[m \e[4m4 Underlined\e[m \e[5m5 Blink\e[m \e[6m6 ???\e[m \e[7m7 Inverted\e[m \e[8m8 Hidden\e[m\n\n"
    printf "If \e[1m%s\e[m is not provided it will reset the display.\n\n" '{$value}'
  fi
}
eight_color() {
    local fgc bgc vals seq0
    if [ "$1" != "-bq" ]; then
        printf "\n\e[1;4m8 Color Escape Value Pallette\e[m\n\n"
        printf "Color escapes are \e[1m%s\e[m\n" '\e[${value};...;${value}m'
        printf "   Values \e[1m30..37\e[m are \e[1mforeground\e[m colors\n"
        printf "   Values \e[1m40..47\e[m are \e[1mbackground\e[m colors\n\n" 
    fi
    for fgc in {30..37}; do
        for bgc in {40..47}; do
            fgc=${fgc#37}
            bgc=${bgc#40}
            vals="${fgc:+$fgc;}${bgc}"
            vals=${vals%%;}
            seq0="${vals:+\e[${vals}m}"
            printf "  %-9s" "${seq0:-(default)}"
            printf " ${seq0}TEXT\e[m"
            printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
        done
        printf "\e[0m\n"
    done
}

if [[ "$1" == "-b" ||  "$1" == "-bq" ]]; then
  eight_color "$1"
  verbose "$1"
elif [[ "$1" == "" || "$1" == "-" ||  "$1" == "-f" ||  "$1" == "-q" ||  "$1" == "-fq" ]]; then
  start=${2:-0}
  end=${3:-255}
  step=${4:-1}
  color=$start
  style="48;5;"
  if [[ "$1" == "-f" || "$1" == "-fq" ]]; then
   style="38;5;"
  fi
  perLine=$(( ( $(tput cols) - 2 ) / 9 ));
  if [[ "$1" != "-q" && "$1" != "-fq" ]]; then
    printf "\n\e[1;4m256 Color Escape Value Pallette\e[0m\n\n"
    printf "   \e[1m%s\e[m for \e[1mbackground\e[m colors\n    \e[1m%s\e[m for \e[1mforeground\e[m colors\n\n" '\e[48;5;${value}m' '\e[38;5;${value}m'
  fi
  while [ $color -le $end ]; do
    printf "\e[m \e[${style}${color}m  %3d  \e[m " $color
    ((color+=step))
    if [ $(( ( ( $color - $start ) / $step ) % $perLine )) -eq 0 ]; then
      printf "\n"
    fi
    done
    printf "\e[m\n"
    verbose "$1"
else
  useage
fi

这对你所使用的终端来说应该是正确的尺寸。对于这个目的来说,这有点夸张,但现在你可以通过参数来控制它的显示方式的许多方面。希望它们都是不言自明的。

评论(1)