转义序列Escape Sequences及Linux echo命令多种颜色显示

2024-05-09 23:52:21

翻阅了很多中文的博文,多数文章是讲echo颜色的用法,本人实在不爱死背,追本溯源,真正看看转义序列是什么?

转义字符,学习过C语言的童鞋都知道,著名的printf函数中支持一些控制字符输出,例如\t 、\n等。我们通常称它们为转义字符。

一、转义序列是什么?

1、维基百科(译者怕翻译偏差大,保留原文)    
In computing, ANSI escape code (or escape sequences) is the method of in-band signaling to control formatting, color, and other output options on video text terminals.    
在计算机界,ANSI转义编码或者转义序列是带内信号(元数据和控制信号,译者注)控制视频文字终端的格式、颜色、其他输出选项的一种方法。    
To encode this formatting information, it embeds certain sequences of bytes into the text, which have to be interpreted specially, not as codes of characters.    
为了编码这些格式信息,它把某种字节的序列嵌入到文本中,该序列必须特别的解释,且不应该作为字符的本义。

Although hardware text terminals have become increasingly rare in the 21st century, the relevance of this standard persists because most terminal emulators interpret at least some of the ANSI escape sequences in the output text.  
尽管硬件字符终端在21世纪已经变得很愈来愈少见,这种标准依然具有实用价值,是因为大多数终端仿真器至少还能解释在输出文本中的一些ANSI转义序列。  
One notable exception is the win32 console component of Microsoft Windows.  
但微软的win32控制台组件就是一个明显的例外。

2、微软的解释    
包含反斜杠 (\) 的字符组合后跟字母或用数字的组合称为 “转义序列”。若要显示换行符、单引号或某些其他字符常数的,必须使用转义序列。 因此转义序列被视为单个字符是有效的作为字符常数。

二、转义序列定义    
Sequence elements    
序列元素    
Escape sequences start with the character ESC (ASCII decimal 27/hex 0x1B/octal 033).    
转义序列使用ESC控制字符开始。    
For two character sequences, the second character is in the range ASCII 64 to 95 (@ to _).    
对于2个字符序列,第二个字符是ASCII的64到95。(@到_,还有所有大写英文字母和[]\^)    
However, most of the sequences are more than two characters, and start with the characters ESC and [ (left bracket).    
然而,序列中大多数是超过2个字符的,并以ESC控制字符和左中括号开始。    
This sequence is called CSI for Control Sequence Introducer (or Control Sequence Initiator). The final character of these sequences is in the range ASCII 64 to 126 (@ to ~).    
序列被称作CSI,即控制序列引导器或者控制序列启动器的简称。这个序列的最后一个字符是在ASCII范围64到126。    
There is a single-character CSI (155/0x9B/0233) as well.    
也还有一个单字符的CSI (155/0x9B/0233)。

The ESC+[ two-character sequence is more often used than the single-character alternative, for details see C0 and C1 control codes.    
ESC[,这两个字符序列比单个字符形式用的更多,细节参看C0和C1控制编码。    
Only the two-character sequence is recognized by devices that support just ASCII (7-bit bytes) or devices that support 8-bit bytes but use the 0x80–0x9F control character range for other purposes.    
那些 仅支持ASCII(7位字节),或者支持8位字节但出于其他用途而使用0x80-0x9F控制字符区域的设备 仅能识别 2字符的序列。    
On terminals that use UTF-8 encoding, both forms take 2 bytes (CSI in UTF-8 is 0xC2, 0x9B) but the ESC+[ sequence is clearer.    
使用UTF-8编码的终端上,两种形式都使用2字节(CSI in UTF-8 is 0xC2, 0x9B),但ESC[序列更加明了。

Though some encodings use multiple bytes per character, the following discussion is restricted to ASCII characters, and so assumes each character is directly represented by a single byte.    
虽然一些编码使用多字节字符,下面的讨论仅针对ASCII字符,因此假定每一个字符由单个字节表示。

30–37    Set text color (foreground)    30 + x, where x is from the color table below    
38    Set xterm-256 text color (foreground)[dubious – discuss]    next arguments are 5;x where x is color index (0..255)    
39    Default text color (foreground)    implementation defined (according to standard)    
40–47    Set background color    40 + x, where x is from the color table below    
48    Set xterm-256 background color    next arguments are 5;x where x is color index (0..255)    
49    Default background color    implementation defined (according to standard)

三、颜色控制序列    
Colors    
Text colors (and SGR parameters in general) are manipulated using CSI n1 [;n2 [; ...]] m sequences, where each n1, n2, ... is an SGR parameter as shown above.    
文本颜色(和SGR (Select Graphic Rendition)参数)使用CSI n1 [;n2 [; ...]] m 序列来处理,如上所示,序列中每一个n1, n2, ...就是一个SGR参数。    
Thus, for instance, you use codes 30+i to specify foreground color, 40+i to specify background color, where i is the number in the desired color's column header in the table below.    
因此,例如,你使用30+i表示前景色,40+i表示背景色, i 就是下表中的颜色数字。

Color table颜色表
Intensity 0 1 2 3 4 5 6 7
Normal Black Red Green Yellow Blue Magenta Cyan White
Bright Black Red Green Yellow Blue Magenta Cyan White

The following examples can be used with the printf utility, where \x1b[ implements the CSI: To switch the foreground color to black, use \x1b[30m; to switch to red, use \x1b[31m; utilizing the "bold" parameter, gray would be \x1b[30;1m; to get bold red, use \x1b[31;1m. To reset colors to their defaults, use \x1b[39;49m (or reset all attributes with \x1b[0m).    
下面的例子使用printf功能,在其中 \x1b[ 实现CSI:    
转换前景色为黑色,使用\x1b[30m    
转换为红色,使用\x1b[31m    
如果使用加粗参数,灰色写作\x1b[30;1m    
获取红色加粗,使用\x1b[31;1m    
重置颜色为缺省值,使用\x1b[39;49m    (或者使用 \x1b[0m 重置所有属性)

\033[0m 重置为正常    
\033[1m 设置高亮度或加粗    
\033[4m 下划线    
\033[5m 闪烁    
\033[7m 反显    
\033[8m 消隐    
\033[30m -- /33[37m 设置前景色    
\033[40m -- /33[47m 设置背景色

控制符ESC的常用表示方法\e、\x1b(\x1B)、\033都可以    
\e 指代Escape,对应八进制\033,对应十六进制\x1b

四、举例:    
使用CentOS 6 的echo命令 
1、$ echo -e "\033[1;31;42mMine is red \e[0m" 
高亮绿底红字    
2、$ echo -e "\033[4mm is red" 
下划线显示,但没有关闭,下划线一直存在,直到遇到第3个示例    
3、$ echo -e "\x1b[0m"    
关闭所有属性,恢复正常

特别感谢,老婆在翻译上给出的大力支持及好的建议。

参看文献    
http://en.wikipedia.org/wiki/ANSI_escape_code

转载于:https://blog.51cto.com/me2xp/1437542

转义序列Escape Sequences及Linux echo命令多种颜色显示相关推荐

  1. linux echo命令_如何在Linux上使用Echo命令

    linux echo命令 Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock The echo c ...

  2. linux命令echo的实现,Linux echo命令的使用及三种实现方式

    先给大家介绍下linux中echo命令的使用 echo是打印变量的值或者给定的字符串, 比如,输入echo hello或者echo "hello"都是在控制台打印出hello单词 ...

  3. linux+echo+权限不够,解决linux echo命令以及linux echo命令提示权限不够的方法

    linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法.下面通过本文给大家介绍linux echo命令以及linux ...

  4. linux下执行php命令echo不输出,linux echo命令以及linux echo命令提示权限不够的方法...

    linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法.下面通过本文给大家介绍linux echo命令以及linux ...

  5. linux echo 命令,Linux echo 命令

    2015-07-06 创建 1.概述 Linux echo 命令    显示文本,加命令选项[-e]时,最好包后面的字符串使用 "" 包起来,以免命令不生效 2.命令格式 echo ...

  6. Linux echo命令和查看环境变量实例

    linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的: echo命令的功能是在显示器上显示一段文字,一般起到一个提示的作用. 该命令的一般格式为: ...

  7. 【Linux】一步一步学Linux——echo命令(203)

    00. 目录 文章目录 00. 目录 01. 命令概述 02. 命令格式 03. 常用选项 04. 参考示例 05. 附录 01. 命令概述 echo命令用于在shell中打印shell变量的值,或者 ...

  8. Linux—echo 命令用法

    目录 一.常用选项 二.常用的转义符 三.使用echo命令打印特效文字 总结 一.常用选项 echo命令我们经常使用的选项有两个,一个是-n,表示输出之后不换行. 一个是-e,表示对于转义字符按对应的 ...

  9. linux echo命令做运算,linux常用计算命令

    一.bc 命令 常用参数选项: -i:强制进入交互式模式: -l:定义使用的标准数学库: -w:对POSIX bc的扩展给出警告信息: -q:不打印正常的GNU bc环境信息: -v:显示指令版本信息 ...

最新文章

  1. tensorflow基于csv数据集实现多元线性回归并预测
  2. Python使用datetime中的timedelta模块实现时间增减:python计算100天后是哪年那月那日?
  3. 数据库报错: SQLCODE: -418, SQLSTATE: 42610, SQLERRMC: null
  4. DeepLab v2
  5. MATLAB自定义函数及局部变量
  6. Bzoj1018 [SHOI2008]堵塞的交通traffic
  7. 不同长度数据项的排序
  8. aelf帮助C#工程师10分钟零门槛搭建DAPP私有链开发环境
  9. 为什么强烈禁止开发人员使用isSuccess作为变量名
  10. b站电脑客户端_B站(哔哩哔哩) 视频批量下载工具#电脑版##更新
  11. OpenCV数据结构
  12. matlab实训心得体会,MatLab实习心得体会
  13. word 远程过程调用失败。 (异常来自 HRESULT:0x800706BE) 解决方法
  14. Matlab图像处理系列3———空间域锐化滤波器
  15. java毕业设计便利店系统mybatis+源码+调试部署+系统+数据库+lw
  16. JavaScript模板引擎
  17. 锁定计算机后忘记密码怎么解开,win10开机密码忘记了怎么办?win10电脑忘记开机密码的解锁方法...
  18. 【教程】创建活动报名二维码(活动报名/会议签到扫码,带微信手机号认证)
  19. 如何利用 XMind 高效学习?
  20. k8s源码分析 pdf_《k8s-1.13版本源码分析》上github

热门文章

  1. mysql口令更换周期_Linux设置口令复杂度和口令定期更换策略
  2. 小米扫地机器人 自动关机_小米扫地机器人1S 真的好用吗?
  3. git的一些知识梳理以及命令操作
  4. 从数学中的虚幻模式到傅里叶变换性质
  5. 四驱麦克纳姆轮运行原理
  6. ULC2003达林顿管阵列STC8K28驱动电路模块
  7. android edittext_Android 支持拖动、缩放的自定义软键盘
  8. logit方程怎么写_一元四次方程的常规解法
  9. php flock 死锁了,php – 防止由flock引起的死锁
  10. HTML语言的含义,HTML是什么意思