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

code Description Example Preview
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

Code Description Example Preview
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), 請參閱兼容性列表以獲取更多信息。

顏色可能因終端配置而異。

Code Color Example Preview
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 背景

Code Color Example Preview
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)組合屬性

終端允許屬性組合。屬性必須用分號(“ ;”)分隔。

Description Code (Bash) Preview
Bold + Underlined echo -e “\e[1;4mBold and Underlined” 在這里插入圖片描述
Bold + Red forground + Green background echo -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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM