shell终端输出内容美化

作者:James Zhu (fatindeed@hotmail.com)

创建日期:2018-05-30

引言

我们在编写shell脚本(如bash/sh等)时,有时会因为输出内容过多而观察不便,因此需要将一些重要信息高亮显示。

ANSI转义序列是一种In-band signaling的转义序列标准,用于控制视频文本终端上的光标位置、颜色和其他选项。在文本中嵌入确定的字节序列,大部分以ESC转义字符和[字符开始,终端会把这些字节序列解释为相应的指令,而不是普通的字符编码。

ANSI转义序列表(不完整列表)

Set

Code

Description

Example

1

Bold/Bright

echo -e "Normal \e[1mBold"

2

Dim

echo -e "Normal \e[2mDim"

4

Underlined

echo -e "Normal \e[4mUnderlined"

5

Blink

echo -e "Normal \e[5mBlink"

7

Reverse

echo -e "Normal \e[7minverted"

8

Hidden

echo -e "Normal \e[8mHidden"

Unset

Code

Description

Example

0

Reset all attributes

echo -e "\e[0mNormal Text"

21

Reset bold/bright

echo -e "Normal \e[1mBold \e[21mNormal"

22

Reset dim

echo -e "Normal \e[2mDim \e[22mNormal"

24

Reset underlined

echo -e "Normal \e[4mUnderlined \e[24mNormal"

25

Reset blink

echo -e "Normal \e[5mBlink \e[25mNormal

27

Reset reverse

echo -e "Normal \e[7minverted \e[27mNormal"

28

Reset hidden

echo -e "Normal \e[8mHidden \e[28mNormal"

Foreground (text)

Code

Description

Example

39

Default foreground color

echo -e "Default \e[39mDefault"

30

Black

echo -e "Default \e[30mBlack"

31

Red

echo -e "Default \e[31mRed"

32

Green

echo -e "Default \e[32mGreen"

33

Yellow

echo -e "Default \e[33mYellow"

34

Blue

echo -e "Default \e[34mBlue"

35

Magenta

echo -e "Default \e[35mMagenta"

36

Cyan

echo -e "Default \e[36mCyan"

37

Light gray

echo -e "Default \e[37mLight gray"

90

Dark gray

echo -e "Default \e[90mDark gray"

91

Light red

echo -e "Default \e[91mLight red"

92

Light green

echo -e "Default \e[92mLight green"

93

Light yellow

echo -e "Default \e[93mLight yellow"

94

Light blue

echo -e "Default \e[94mLight blue"

95

Light magenta

echo -e "Default \e[95mLight magenta"

96

Light cyan

echo -e "Default \e[96mLight cyan"

97

White

echo -e "Default \e[97mWhite"

Background

Code

Description

Example

49

Default background color

echo -e "Default \e[49mDefault"

40

Black

echo -e "Default \e[40mBlack"

41

Red

echo -e "Default \e[41mRed"

42

Green

echo -e "Default \e[42mGreen"

43

Yellow

echo -e "Default \e[43mYellow"

44

Blue

echo -e "Default \e[44mBlue"

45

Magenta

echo -e "Default \e[45mMagenta"

46

Cyan

echo -e "Default \e[46mCyan"

47

Light gray

echo -e "Default \e[47mLight gray"

100

Dark gray

echo -e "Default \e[100mDark gray"

101

Light red

echo -e "Default \e[101mLight red"

102

Light green

echo -e "Default \e[102mLight green"

103

Light yellow

echo -e "Default \e[103mLight yellow"

104

Light blue

echo -e "Default \e[104mLight blue"

105

Light magenta

echo -e "Default \e[105mLight magenta"

106

Light cyan

echo -e "Default \e[106mLight cyan"

107

White

echo -e "Default \e[107mWhite"

参考资料

Last updated