2

Shell中色彩处理

 1 year ago
source link: https://blog.51cto.com/zengyi/5919821
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一、echo命令结果高亮显示

Shell脚本中echo命令显示内容带颜色高亮显示时,需要使用参数-e。

命令使用格式1:echo -e"\033[背景颜色;文字颜色m要输出的内容\033[0m"。

命令使用格式2:echo -e"\e[背景颜色;文字颜色m要输出的内容\e[0m"。

比如:白底黑字。

[root@centos7-client ~]# echo -e "\033[47;30m hello world\033[0m"
[root@centos7-client ~]# echo -e "\e[47;30m hello world\e[0m"

效果如图所示:

Shell中色彩处理_文字颜色

注意:其中47的位置代表背景颜色,30的位置代表文字颜色,0m是清除所有格式。

(1)背景颜色和文字颜色之间是“;”。

(2)文字颜色后面有一个字母m。

(3)字符串前后可以没有空格,如果有的话,输出也同样有空格。

(4)echo显示带颜色,需要使用参数-e,允许对后面列出的加反斜线转义的字符进行解释。

二、echo命令的控制选项

\033[0m:关闭所有属性。

\033[1m:设置高亮度,加粗显示。

\033[5m:设置闪烁状态。

[root@centos7-client ~]# echo -e "\e[42;34m hello world\e[5m"

执行后,发现后期所有输出都带闪烁状态,如图所示:

Shell中色彩处理_文字颜色_02

[root@centos7-client ~]# echo -e "\e[42;34m hello world\e[0m" 可以使用\033[0m 关闭所有属性,或者使用ls。

三、常见Shell脚本输出的文字颜色和背景颜色

Shell中色彩处理_文字颜色_03
Shell中色彩处理_背景颜色_04
Shell中色彩处理_文字颜色_05

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK