shell bash终端中输出的颜色和格式详解(超详细)

ANSI/VT100终端和终端仿真器不只是能够显示黑色和白色文本; 由于转义序列,它们可以显示颜色和格式化文本。这些序列由Escape字符(通常用“^[” 或"<Esc>"表示)组成,后跟一些其他字符:“<Esc>[FormatCodem”。
在Bash中,可以使用以下语法获取字符:
\e
\033
\x1B
例子:

代码(Bash)Preview
echo -e “\ e [31mHello World \ e [0m”hello world
echo -e “\ 033 [31mHello \ e [0m World”hello world

注意:

  1. 该命令的-e选项echo启用转义序列的解析。
  2. "\e[0m"序列删除所有属性(格式和颜色)。在每个彩色文本的末尾添加它是个好主意。;)
  3. 本页中的示例使用Bash,但ANSI/VT100转义序列可用于各种编程语言。

1) 格式

1.1 Set

codeDescriptionExamplePreview
1粗体/高亮echo -e “Normal \ e [1mBold”在这里插入图片描述
2变暗echo -e “Normal \ e [2mDim”在这里插入图片描述
4下划线echo -e “Normal \ e [4mUnderlined”在这里插入图片描述
5闪烁echo -e “Normal \ e [5mBlink”在这里插入图片描述
7反转(反转前景色和背景色)echo -e “Normal \ e [7minverted”在这里插入图片描述
8隐藏(对密码有用)echo -e “Normal \ e [8mHidden”在这里插入图片描述

1.2 Reset

CodeDescriptionExamplePreview
0重置所有属性echo -e “\e[0mNormal Text”在这里插入图片描述
21重置粗体/高亮echo -e “Normal \e[1mBold \e[21mNormal”在这里插入图片描述
22重置变暗echo -e “Normal \e[2mDim \e[22mNormal”在这里插入图片描述
24重置下划线echo -e “Normal \e[4mUnderlined \e[24mNormal”在这里插入图片描述
25重置闪烁echo -e “Normal \e[5mBlink \e[25mNormal”在这里插入图片描述
27重置反显echo -e “Normal \e[7minverted \e[27mNormal”在这里插入图片描述
28重置隐藏echo -e “Normal \e[8mHidden \e[28mNormal”在这里插入图片描述

2)8/16 Colors

2.1 前景(文字)

以下颜色适用于大多数终端和终端仿真器2), 请参阅兼容性列表以获取更多信息。

颜色可能因终端配置而异。

CodeColorExamplePreview
39默认前景色echo -e “Default \ e [39mDefault”在这里插入图片描述
30黑色echo -e “Default \ e [30mBlack”在这里插入图片描述
31红色echo -e “Default \ e [31mRed”在这里插入图片描述
32绿色echo -e “Default \ e [32mGreen”在这里插入图片描述
33黄色echo -e “Default \ e [33mYellow”在这里插入图片描述
34蓝色echo -e “Default \ e [34mBlue”在这里插入图片描述
35品红echo -e “Default \ e [35mMagenta”在这里插入图片描述
36青色echo -e “Default \ e [36mCyan”在这里插入图片描述
37浅灰echo -e “Default \ e [37mLight grey”在这里插入图片描述
90深灰色echo -e “Default \ e [90mDark grey”在这里插入图片描述
91红灯echo -e “默认\ e [91mLight red”在这里插入图片描述
92浅绿色echo -e “Default \ e [92mLight green”在这里插入图片描述
93淡黄色echo -e “Default \ e [93mLight yellow”在这里插入图片描述
94浅蓝echo -e “Default \ e [94mLight blue”在这里插入图片描述
95浅洋红色echo -e “Default \ e [95mLight magenta”在这里插入图片描述
96浅青色echo -e “Default \ e [96mLight cyan”在这里插入图片描述
97白色echo -e “Default \ e [97mWhite”在这里插入图片描述

2.2 背景

CodeColorExamplePreview
49默认背景颜色echo -e “Default \ e [49mDefault”在这里插入图片描述
40黑色echo -e “Default \ e [40mBlack”在这里插入图片描述
41红色echo -e “Default \ e [41mRed”在这里插入图片描述
42绿色echo -e “Default \ e [42mGreen”在这里插入图片描述
43黄色echo -e “Default \ e [43mYellow”在这里插入图片描述
44蓝色echo -e “Default \ e [44mBlue”在这里插入图片描述
45品红echo -e “Default \ e [45mMagenta”在这里插入图片描述
46青色echo -e “Default \ e [46mCyan”在这里插入图片描述
47浅灰echo -e “Default \ e [47mLight grey”在这里插入图片描述
100深灰色echo -e “Default \ e [100mDark grey”在这里插入图片描述
101红灯echo -e “默认\ e [101mLight red”在这里插入图片描述
102浅绿色echo -e “默认\ e [102mLight green”在这里插入图片描述
103淡黄色echo -e “默认\ e [103mLight yellow”在这里插入图片描述
104浅蓝echo -e “Default \ e [104mLight blue”在这里插入图片描述
105浅洋红色echo -e “Default \ e [105mLight magenta”在这里插入图片描述
106浅青色echo -e “Default \ e [106mLight cyan”在这里插入图片描述
107白色echo -e “Default \ e [107mWhite”在这里插入图片描述

3)88/256颜色

某些终端(参见兼容性列表)可以支持88或256种颜色。以下是允许您使用它们的控制序列。

注意:颜色编号256仅由vte支持(GNOME终端,XFCE4终端,Nautilus终端,终结者…)。

注意2:88色终端(如rxvt)与256色终端的颜色图不同。要显示88色终端颜色映射,请在88色终端中运行“ 256-colors.sh ”脚本。

3.1 前景(文字)

要使用前景中的256种颜色之一(文本颜色),控制序列为“ <Esc>[38;5;ColorNumberm ”,其中ColorNumber是以下颜色之一:
在这里插入图片描述
Example:

echo -e "\e[38;5;82mHello \e[38;5;198mWorld"

在这里插入图片描述

for i in {16..21} {21..16} ; do echo -en "\e[38;5;${i}m#\e[0m" ; done ; echo

在这里插入图片描述

3.2 背景色

要在背景上使用256种颜色中的一种,控制序列为“ <Esc>[48;5;ColorNumberm ”,其中ColorNumber是以下颜色之一:
在这里插入图片描述
Example:

echo -e "\e[40;38;5;82m Hello \e[30;48;5;82m World \e[0m"

在这里插入图片描述

for i in {16..21} {21..16} ; do echo -en "\e[48;5;${i}m \e[0m" ; done ; echo

在这里插入图片描述

4)组合属性

终端允许属性组合。属性必须用分号(“ ;”)分隔。

DescriptionCode (Bash)Preview
Bold + Underlinedecho -e “\e[1;4mBold and Underlined”在这里插入图片描述
Bold + Red forground + Green backgroundecho -e “\e[1;31;42m Yes it is awful \e[0m”在这里插入图片描述

5)终端兼容性

在这里插入图片描述

表中使用的符号:
“ ok”:终端支持。
“ ~”:终端以特殊方式支持。
“ -”:终端根本不支持。

6)示例程序

6.1 Colors and formatting (16 colors)

以下shell脚本显示了许多可能的属性组合(但不是全部,因为它一次只使用一个格式属性)。

#!/bin/bash
 
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
 
#Background
for clbg in {40..47} {100..107} 49 ; do
	#Foreground
	for clfg in {30..37} {90..97} 39 ; do
		#Formatting
		for attr in 0 1 2 4 5 7 ; do
			#Print the result
			echo -en "\e[${attr};${clbg};${clfg}m ^[${attr};${clbg};${clfg}m \e[0m"
		done
		echo #Newline
	done
done 
exit 0

在这里插入图片描述

6.2 256 colors

以下脚本显示某些终端和终端仿真器(如XTerm和GNOME Terminal)上可用的256种颜色。

#!/bin/bash
 
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
 
for fgbg in 38 48 ; do # Foreground / Background
    for color in {0..255} ; do # Colors
        # Display the color
        printf "\e[${fgbg};5;%sm  %3s  \e[0m" $color $color
        # Display 6 colors per lines
        if [ $((($color + 1) % 6)) == 4 ] ; then
            echo # New line
        fi
    done
    echo # New line
done
 
exit 0

在这里插入图片描述

参考

Linux console codes manual (’‘man console_codes’’)
XTerm Control Sequences

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小麦大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值