linux 中 echo的颜色输出

发布时间 2023-06-22 20:30:43作者: 小鲨鱼2018

 

echo: 字体颜色  和 背景颜色。

常见的字体颜色:重置=0,黑色=30,红色=31,绿色=32,黄色=33,蓝色=34,紫色=35,天蓝色=36,白色=37。

常见的背景颜色:重置=0,黑色=40,红色=41,绿色=42,黄色=43,蓝色=44,紫色=45,天蓝色=46,白色=47

字体控制选项:1表示高亮,4表示下划线,5表示闪烁等。

因为需要使用特殊符号,所以需要配合-e选项来识别特殊符号。

 

001、

[root@PC1 test01]# echo -e "\e[1;41m Red Bcakground \e[0m"     ## 其中1表示高亮; 41m表示背景红色
 Red Bcakground
[root@PC1 test01]# echo -e "\e[4;41m Red Bcakground \e[0m"     ## 其中4表示下划线
 Red Bcakground

 

002、改变背景色;42 绿色背景

[root@PC1 test01]# echo -e "\e[1;41m Red Bcakground \e[0m"
 Red Bcakground
[root@PC1 test01]# echo -e "\e[1;42m Red Bcakground \e[0m"   ## 42表示绿色背景,1高亮
 Red Bcakground
[root@PC1 test01]# echo -e "\e[4;42m Red Bcakground \e[0m"   ## 4表示下划线
 Red Bcakground

 

003、测试背景色

[root@PC1 test01]# echo -e "\e[1;41m Red Bcakground \e[0m"   ## 41 红色
 Red Bcakground
[root@PC1 test01]# echo -e "\e[1;40m Red Bcakground \e[0m"   ## 40 黑色
 Red Bcakground
[root@PC1 test01]# echo -e "\e[1;0m Red Bcakground \e[0m"    ## 0 重置
 Red Bcakground
[root@PC1 test01]# echo -e "\e[1;42m Red Bcakground \e[0m"   ## 42 绿色
 Red Bcakground
[root@PC1 test01]# echo -e "\e[1;43m Red Bcakground \e[0m"   ## 43  黄色
 Red Bcakground
[root@PC1 test01]# echo -e "\e[1;44m Red Bcakground \e[0m"   ## 44  蓝色
 Red Bcakground
[root@PC1 test01]# echo -e "\e[1;45m Red Bcakground \e[0m"   ## 45 紫色
 Red Bcakground
[root@PC1 test01]# echo -e "\e[1;46m Red Bcakground \e[0m"   ## 46 天蓝色
 Red Bcakground
[root@PC1 test01]# echo -e "\e[1;47m Red Bcakground \e[0m"   ## 47 白色
 Red Bcakground
[root@PC1 test01]# echo -e "\e[5;47m Red Bcakground \e[0m"   ## 5 表示闪烁
 Red Bcakground

 

004、测试字体颜色

[root@PC1 test01]# echo -e "\e[1;30m linux \e[0m"   ## 30 黑色
 linux
[root@PC1 test01]# echo -e "\e[1;31m linux \e[0m"   ## 31 红色
 linux
[root@PC1 test01]# echo -e "\e[1;32m linux \e[0m"   ## 32 绿色
 linux
[root@PC1 test01]# echo -e "\e[1;33m linux \e[0m"   ## 33 黄色
 linux
[root@PC1 test01]# echo -e "\e[1;34m linux \e[0m"   ## 34 蓝色
 linux
[root@PC1 test01]# echo -e "\e[1;35m linux \e[0m"   ## 35 紫色
 linux
[root@PC1 test01]# echo -e "\e[1;36m linux \e[0m"   ## 36 天蓝色
 linux
[root@PC1 test01]# echo -e "\e[1;37m linux \e[0m"   ## 37 白色
 linux

 

005、任意一个\e都可以使用\033进行替换

[root@PC1 test01]# echo -e "\e[1;47m Red Bcakground \e[0m"
 Red Bcakground
[root@PC1 test01]# echo -e "\033[1;47m Red Bcakground \033[0m"
 Red Bcakground