Mic*_*ant 173 colors bash prompt
我在 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
Run Code Online (Sandbox Code Playgroud)
在哪里可以找到我可以使用的颜色代码列表?
我查看了Colorize Bash Console Color,但它没有回答我关于实际代码列表的问题。
如果还有一个更易读的表格,那就太好了。
gol*_*cks 240
这些是ANSI 转义序列;该链接指向颜色代码图表,但该维基百科页面上还有其他有趣的内容。并非所有这些都在(例如)普通的 Linux 控制台上工作。
这是不正确的:
\033]00m\] # white
0将终端重置为其默认值(可能是白色)。白色前景的实际代码是 37。此外,结尾处的转义右括号 ( \]) 不是颜色序列的一部分(请参阅下面的最后几段以了解其设置提示的目的)。
请注意,某些 GUI 终端允许您指定自定义的配色方案。这会影响输出。
这里有一个列表,它添加了我以前从未见过的 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
Run Code Online (Sandbox Code Playgroud)
另外,如果你有一个 256 色的 GUI 终端(我认为现在大部分都是这样),你可以从这个图表中应用颜色:

选择这些的 ANSI 序列,使用左下角的数字,从38;5;前景和48;5;背景开始,然后是颜色编号,例如:
echo -e "\\033[48;5;95;38;5;214mhello world\\033[0m"
Run Code Online (Sandbox Code Playgroud)
在棕褐色上给我一个浅橙色(意思是,颜色图表大致近似)。
您可以在此图表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
Run Code Online (Sandbox Code Playgroud)
输出是不言自明的。
某些系统将 $TERM 变量设置为xterm-256color如果您在 256 色终端上通过/etc/profile. 在其他情况下,您应该能够配置您的终端以使用它。这将使 TUI 应用程序知道有 256 种颜色,并允许您将这样的内容添加到您的~/.bashrc:
if [[ "$TERM" =~ 256color ]]; then
PS1="MyCrazyPrompt..."
fi
Run Code Online (Sandbox Code Playgroud)
请注意,在提示中使用颜色转义序列时,应将它们括在转义(带\前缀)方括号中,如下所示:
PS1="\[\033[01;32m\]MyPrompt: \[\033[0m\]"
Run Code Online (Sandbox Code Playgroud)
请注意,[颜色序列的 内部没有转义,但封闭的却转义了。后者的目的是向 shell 指示所包含的序列不计入提示的字符长度。如果该计数错误,当您向后滚动历史记录时会发生奇怪的事情,例如,如果它太长,最后滚动的字符串的多余长度将显示在您的提示中,您将无法退格进入它(它被忽略的方式与提示相同)。
另请注意,如果您想在每次使用提示时都包含命令运行的输出(而不是在设置提示时只包含一次),您应该将其设置为带单引号的文字字符串,例如:
PS1='\[\033[01;32m\]$(date): \[\033[0m\]'
Run Code Online (Sandbox Code Playgroud)
尽管如果您对使用 bash 的特殊\d或\D{format}提示转义感到满意,这不是一个很好的例子——这不是问题的主题,但可以man bash在PROMPTING. 还有各种其他有用的转义,例如\w当前目录、\u当前用户等。
1. 该图表的主要部分,颜色 16 - 231(注意它们不是按数字顺序排列)是一个 6 x 6 x 6 RGB 颜色立方体。“颜色立方体”是指RGB颜色空间可以用一个三维阵列来表示(一根轴代表红色,一根代表绿色,一根代表蓝色)。这里立方体中的每种颜色都可以表示为 6 x 6 x 6 数组中的坐标,图表中的索引是这样计算的:
16 + R * 36 + G * 6 + B
Run Code Online (Sandbox Code Playgroud)
立方体中的第一种颜色(图表中的索引 16)是黑色 (RGB 0, 0, 0)。您可以在 shell 脚本中使用此公式:
#!/bin/sh
function RGBcolor {
echo "16 + $1 * 36 + $2 * 6 + $3" | bc
}
fg=$(RGBcolor 1 0 2) # Violet
bg=$(RGBcolor 5 3 0) # Bright orange.
echo -e "\\033[1;38;5;$fg;48;5;${bg}mviolet on tangerine\\033[0m"
Run Code Online (Sandbox Code Playgroud)
Mic*_*ant 56
看起来至少有一些列表是:
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
Run Code Online (Sandbox Code Playgroud)
基于https://wiki.archlinux.org/index.php/Color_Bash_Prompt
Pla*_*rob 37
如果有帮助,我编写了一个 bash 函数,可以向您显示所有颜色。
function colorgrid( )
{
iter=16
while [ $iter -lt 52 ]
do
second=$[$iter+36]
third=$[$second+36]
four=$[$third+36]
five=$[$four+36]
six=$[$five+36]
seven=$[$six+36]
if [ $seven -gt 250 ];then seven=$[$seven-251]; fi
echo -en "\033[38;5;$(echo $iter)m? "
printf "%03d" $iter
echo -en " \033[38;5;$(echo $second)m? "
printf "%03d" $second
echo -en " \033[38;5;$(echo $third)m? "
printf "%03d" $third
echo -en " \033[38;5;$(echo $four)m? "
printf "%03d" $four
echo -en " \033[38;5;$(echo $five)m? "
printf "%03d" $five
echo -en " \033[38;5;$(echo $six)m? "
printf "%03d" $six
echo -en " \033[38;5;$(echo $seven)m? "
printf "%03d" $seven
iter=$[$iter+1]
printf '\r\n'
done
}
Run Code Online (Sandbox Code Playgroud)
您可以将其放入 .bashrc / .bash_profile / .bash_aliases 或将其另存为脚本并以这种方式运行。您可以使用颜色来改变颜色,就像我在下面使用我的名字一样。
我通过这样做在我的 .bash_profile 中更改了我的名字:
if [ "$USER" = "plasmarob" ]; then
p="\[\033[01;38;5;52m\]p"
l="\[\033[01;38;5;124m\]l"
a="\[\033[01;38;5;196m\]a"
s="\[\033[01;38;5;202m\]s"
m="\[\033[01;38;5;208m\]m"
a2="\[\033[01;38;5;214m\]a"
r="\[\033[01;38;5;220m\]r"
o="\[\033[01;38;5;226m\]o"
b="\[\033[01;38;5;228m\]b"
local __user_and_host="$p$l$a$s$m$a2$r$o$b"
else
local __user_and_host="\[\033[01;36m\]\u"
fi
...
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
Run Code Online (Sandbox Code Playgroud)
请注意,字符串中的 01 前缀 like\[\033[01;38;5;214m\]a
将其设置为粗体。
kro*_*owe 12
另一个脚本,如 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
Run Code Online (Sandbox Code Playgroud)
这应该适合您正在使用的终端的大小。为此目的有点过头,但现在您可以通过参数控制其显示方式的许多方面。希望它们都是不言自明的。