echo -e 可以控制字體顏色和背景顏色輸出
從一個例子開始:
# echo -e "\e[1;33;41m test content \e[0m"
輸出效果:

1. \e 轉義起始符,定義一個轉義序列, 可以使用 \033代替 2. [ 表示開始定義顏色 3. 1表示高亮,33表示字體顏色為黃色,45表示背景色為紅色 4. “test content” 屬於文字內容 5. m 轉義終止符,表示顏色定義完畢 6. 再次使用 \e[ ,表示再次開啟顏色定義,0表示使用默認的顏色,m表示顏色定義結束,所以 \e[0m 的作用是恢復之前的配色方案
一、字體顏色
字體顏色:30——37
默認=0,黑色=30,紅色=31,綠色=32,黃色=33,藍色=34,紫色=35,天藍色=36,白色=3
[root@k8s-node02 test]# echo -e "\e[30m test content黑 \e[0m" test content黑 [root@k8s-node02 test]# echo -e "\e[31m test content紅 \e[0m" test content紅 [root@k8s-node02 test]# echo -e "\e[32m test content綠 \e[0m" test content綠 [root@k8s-node02 test]# echo -e "\e[33m test content黃 \e[0m" test content黃 [root@k8s-node02 test]# echo -e "\e[34m test content藍 \e[0m" test content藍 [root@k8s-node02 test]# echo -e "\e[35m test content紫 \e[0m" test content紫 [root@k8s-node02 test]# echo -e "\e[36m test content天藍 \e[0m" test content天藍 [root@k8s-node02 test]# echo -e "\e[37m test content白 \e[0m" test content白
輸出效果:

二、字背景顏色
字背景顏色:40——47
默認=0,黑色=40,紅色=41,綠色=42,黃色=43,藍色=44,紫色=45,天藍色=46,白色=47
[root@k8s-node02 test]# echo -e "\e[40m test content黑 \e[0m" test content黑 [root@k8s-node02 test]# echo -e "\e[41m test content紅 \e[0m" test content紅 [root@k8s-node02 test]# echo -e "\e[42m test content綠 \e[0m" test content綠 [root@k8s-node02 test]# echo -e "\e[43m test content黃 \e[0m" test content黃 [root@k8s-node02 test]# echo -e "\e[44m test content藍 \e[0m" test content藍 [root@k8s-node02 test]# echo -e "\e[45m test content紫 \e[0m" test content紫 [root@k8s-node02 test]# echo -e "\e[46m test content天藍 \e[0m" test content天藍 [root@k8s-node02 test]# echo -e "\e[47m test content白 \e[0m" test content白
輸出效果:

三、黑底彩色
黑底彩色:90——97
黑=90 深紅=91 綠=92 黃色=93 藍色=94 紫色=95 深綠=96 白色=97
[root@k8s-node02 test]# echo -e "\e[90m test content黑 \e[0m" test content黑 [root@k8s-node02 test]# echo -e "\e[91m test content紅 \e[0m" test content紅 [root@k8s-node02 test]# echo -e "\e[92m test content綠 \e[0m" test content綠 [root@k8s-node02 test]# echo -e "\e[93m test content黃 \e[0m" test content黃 [root@k8s-node02 test]# echo -e "\e[94m test content藍 \e[0m" test content藍 [root@k8s-node02 test]# echo -e "\e[95m test content紫 \e[0m" test content紫 [root@k8s-node02 test]# echo -e "\e[96m test content天藍 \e[0m" test content天藍 [root@k8s-node02 test]# echo -e "\e[97m test content白 \e[0m" test content白
輸出結果:

四、字體控制選項:
\033[0m 關閉所有屬性
\033[1m 設置高亮度
\033[4m 下划線
\033[5m 閃爍
\033[7m 反顯,撞色顯示,顯示為白色黑底,或者顯示為黑底白字
\033[8m 消影,字符顏色將會與背景顏色相同
\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 顯示光標
五、實例演示
1.示例1:定義顏色變量
# vim color_test1.sh
#!/bin/bash # 定義顏色變量,\033、\e、\E是等價的,都是轉義起始符 RED='\e[1;31m' # 紅 GREEN='\e[1;32m' # 綠 YELLOW='\033[1;33m' # 黃 BLUE='\E[1;34m' # 藍 PINK='\E[1;35m' # 粉紅 RES='\033[0m' # 清除顏色 echo -e "${RED} Red ${RES}" echo -e "${YELLOW} Yellow ${RES}" echo -e "${BLUE} Blue ${RES}" echo -e "${GREEN} Green ${RES}" echo -e "${PINK} Pink ${RES}"
# source color_test1.sh

2.示例2:定義顏色動作
# vim color_test2.sh
#!/bin/bash # 定義顏色動作 SETCOLOR_SUCCESS="echo -en \\E[1;32m" SETCOLOR_FAILURE="echo -en \\E[1;31m" SETCOLOR_WARNING="echo -en \\E[1;33m" SETCOLOR_NORMAL="echo -en \\E[0;39m" # 使用時直接調用顏色動作,跟上相應的內容 $SETCOLOR_SUCCESS && echo test1 $SETCOLOR_FAILURE && echo test2 $SETCOLOR_WARNING && echo test3 $SETCOLOR_NORMAL && echo test4
需要注意其中的一些細節:
(1)需要增加-n選項,這樣引用時不會出現換行的問題。
(2)\\本質是\,在雙引號中反斜線符號一定要寫成\\。 (3)引用變量要放到其他語句前面,並使用&&連接。
# source color_test2.sh

六、tput命令
tput 命令會利用 terminfo 數據庫中的信息,來控制和更改我們的終端,比如控制光標、更改文本屬性、控制屏幕,以及為文本塗色。
其中,為文本塗色的方法是:
1 tput setab:用於設置背景色 2 tput setaf:用於設置前景色 3 sgr0:表示顏色重置
顏色定義如下:

改寫 實例演示中的 color_test1.sh
#!/bin/bash # 定義顏色變量,\033、\e、\E是等價的,都是轉義起始符 RED=$(tput setaf 1) # 紅 GREEN=$(tput setaf 2) # 綠 YELLOW=$(tput setaf 3) # 黃 BLUE=$(tput setaf 4) # 藍 PINK=$(tput setaf 5) # 粉紅 RES=$(tput sgr0) # 清除顏色 echo -e "${RED} Red ${RES}" echo -e "${YELLOW} Yellow ${RES}" echo -e "${BLUE} Blue ${RES}" echo -e "${GREEN} Green ${RES}" echo -e "${PINK} Pink ${RES}"
# source color_test1.sh

