avatar

目录
设置终端输出字符的属性(颜色+格式+超链接)

转义字符

ANSI / VT100终端和终端仿真器不仅能够显示黑白文本,还可以显示彩色和格式化的文本。该功能是通过转义序列(escape sequences)实现的。转义序列由转义字符(通常由^[<Esc>表示)以及其他一些字符组成:

bash
1
<Esc> [ FormatCode m

在bash中<Esc>可以用以下字符方式表示:

  • \e
  • \033
  • \x1B

字体颜色

8/16 Colors

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

注:颜色可能会根据终端配置而有所不同

前景色 (文字)

代码 颜色 例子 预览
39 默认 echo -e "Default \e[39mDefault" Default Default
30 echo -e "Default \e[30mBlack" Default Black
31 echo -e "Default \e[31mRed" Default Red
32 绿 echo -e "Default \e[32mGreen" Default Green
33 echo -e "Default \e[33mYellow" Default Yellow
34 echo -e "Default \e[34mBlue" Default Blue
35 echo -e "Default \e[35mMagenta" Default Magenta
36 echo -e "Default \e[36mCyan" Default Cyan
37 亮灰 echo -e "Default \e[37mLight gray" Default Light gray
90 暗灰 echo -e "Default \e[90mDark gray" Default Dark gray
91 亮红 echo -e "Default \e[91mLight red" Default Light red
92 亮绿 echo -e "Default \e[92mLight green" Default Light green
93 亮黄 echo -e "Default \e[93mLight yellow" Default Light yellow
94 亮蓝 echo -e "Default \e[94mLight blue" Default Light blue
95 亮紫 echo -e "Default \e[95mLight magenta" Default Light magenta
96 亮青 echo -e "Default \e[96mLight cyan" Default Light cyan
97 echo -e "Default \e[97mWhite" Default White

背景色

代码 颜色 例子 预览
49 默认 echo -e "Default \e[49mDefault" Default Default
40 echo -e "Default \e[40mBlack" Default Black
41 echo -e "Default \e[41mRed" Default Red
42 绿 echo -e "Default \e[42mGreen" Default Green
43 echo -e "Default \e[43mYellow" Default Yellow
44 echo -e "Default \e[44mBlue" Default Blue
45 echo -e "Default \e[45mMagenta" Default Magenta
46 echo -e "Default \e[46mCyan" Default Cyan
47 亮灰 echo -e "Default \e[47mLight gray" Default Light gray
100 暗灰 echo -e "Default \e[100mDark gray" Default Dark gray
101 亮红 echo -e "Default \e[101mLight red" Default Light red
102 亮绿 echo -e "Default \e[102mLight green" Default Light green
103 亮黄 echo -e "Default \e[103mLight yellow" Default Light yellow
104 亮蓝 echo -e "Default \e[104mLight blue" Default Light blue
105 亮紫 echo -e "Default \e[105mLight magenta" Default Light magenta
106 亮青 echo -e "Default \e[106mLight cyan" Default Light cyan
107 echo -e "Default \e[107mWhite" Default White

88/256 Colors

某些终端(请参阅兼容性表)可以支持88或256种颜色。

注¹:仅vte(GNOME终端,XFCE4终端,Nautilus终端,终结器等)支持颜色数字256。

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

前景色(文字)

控制序列为:

bash
1
<Esc> [38;5; ColorNumber m

XTerm 256 color list (foreground)

例子 (Bash) 预览
echo -e "\e[38;5;82mHello \e[38;5;198mWorld" Hello World
for i in {16..21} {21..16} ; do echo -en "\e[38;5;${i}m#\e[0m" ; done ; echo Blue gradiant

背景色

控制序列为:

Code
1
<Esc> [48;5; ColorNumber m

XTerm 256 color list (background)

例子 (Bash) 预览
echo -e "\e[40;38;5;82m Hello \e[30;48;5;82m World \e[0m" Hello World
for i in {16..21} {21..16} ; do echo -en "\e[48;5;${i}m \e[0m" ; done ; echo Blue gradiant

字体格式

设置格式

代码 格式 例子 预览
1 加粗/变亮 echo -e "Normal \e[1mBold" Normal Bold
2 变暗 echo -e "Normal \e[2mDim" Normal Dim
4 下划线 echo -e "Normal \e[4mUnderlined" Normal Underlined
5 Blink 1) echo -e "Normal \e[5mBlink" Normal Blink
7 反色 echo -e "Normal \e[7minverted" Normal inverted
8 消隐(常用于密码输入) echo -e "Normal \e[8mHidden" Normal Hidden

重置格式

代码 格式 例子 预览
0 重置所有属性 echo -e "\e[0mNormal Text" Normal Text
21 重置加粗/变亮 echo -e "Normal \e[1mBold \e[21mNormal" Normal Bold Normal
22 重置变暗 echo -e "Normal \e[2mDim \e[22mNormal" Normal Dim Normal
24 重置下划线 echo -e "Normal \e[4mUnderlined \e[24mNormal" Normal Underlined Normal
25 重置闪烁 echo -e "Normal \e[5mBlink \e[25mNormal" Normal Blink Normal
27 重置反色 echo -e "Normal \e[7minverted \e[27mNormal" Normal inverted Normal
28 重置消隐 echo -e "Normal \e[8mHidden \e[28mNormal" Normal Hidden Normal

属性组合

终端允许属性组合。属性之间必须用分号分隔

描述 例子 (Bash) 预览
粗体+下划线 echo -e "\e[1;4mBold and Underlined" Bold and Underlined
粗体+绿底红字(丑) echo -e "\e[1;31;42m Yes it is awful \e[0m" Yes it is awful

光标控制

常用于终端GUI

Code
1
2
3
4
5
6
7
8
9
10
11
\033[nA 光标上移n行 
\033[nB 光标下移n行
\033[nC 光标右移n行
\033[nD 光标左移n行
\033[y;xH设置光标位置
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\033[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\033[?25h 显示光标

超链接

控制序列如下:

Code
1
\e]8;; link \a text \e]8;;\a

例子

bash
1
echo -e "\e]8;;http://taqini.space\aThis is taqini's blog\e]8;;\a"

终端兼容性表

Terminal Formatting Colors Comment
Bold Dim Underlined Blink invert Hidden 8 16 88 256
aTerm ok - ok - ok - ok ~ - - Lighter background instead of blink.
Eterm ~ - ok - ok - ok ~ - ok Lighter color instead of Bold. Lighter background instead of blink. Can overline a text with the “^[[6m” sequence.
GNOME Terminal ok ok ok ok ok ok ok ok - ok Strikeout with the “^[[9m” sequence.
Guake ok ok ok ok ok ok ok ok - ok Strikeout with the “^[[9m” sequence.
Konsole ok - ok ok ok - ok ok - ok
Nautilus Terminal ok ok ok ok ok ok ok ok - ok Strikeout with the “^[[9m” sequence.
rxvt ok - ok ~ ok - ok ok ok - If the background is not set to the default color, Blink make it lighter instead of blinking. Support of italic text with the “^[[3m” sequence.
Terminator ok ok ok - ok ok ok ok - ok Strikeout with the “^[[9m” sequence.
Tilda ok - ok ok ok - ok ok - - Underline instead of Dim. Convert 256-colors in 16-colors.
XFCE4 Terminal ok ok ok ok ok ok ok ok - ok Strikeout with the “^[[9m” sequence.
XTerm ok - ok ok ok ok ok ok - ok
xvt ok - ok - ok - - - - -
Linux TTY ok - - - ok - ok ~ - - Specials colors instead of Dim and Underlined. Lighter background instead of Blink, Bug with 88/256 colors.
VTE Terminal 3) ok ok ok ok ok ok ok ok - ok Strikeout with the “^[[9m” sequence.

表中使用的符号:

  • ok:由终端支持。
  • ~:由终端以特殊方式支持。
  • -:终端完全不支持。

Links

bash:tip_colors_and_formatting - FLOZz’ MISC

url - Terminal - create hyperlinks - Unix & Linux Stack Exchange

文章作者: TaQini
文章链接: http://taqini.space/2020/04/30/shell-output-control-code/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 TaQini
打赏
  • Wechat
    Wechat
  • Alipay
    Alipay

评论