输出红色
\u001b[0;31mNot running\u001b[0m\n
输出绿色
\u001b[0;32mRunning [2640]\u001b[0m\n
why?
解释是这样的:
The original specification only had 8 colors, and just gave them names. The SGR parameters 30-37 selected the foreground color, while 40-47 selected the background. Quite a few terminals implemented "bold" (SGR code 1) as a brighter color rather than a different font, thus providing 8 additional foreground colors. Usually you could not get these as background colors, though sometimes inverse video (SGR code 7) would allow that. Examples: to get black letters on white background use ESC[30;47m, to get red use ESC[31m, to get bright red use ESC[31;1m. To reset colors to their defaults, use ESC[39;49m (not supported on some terminals), or reset all attributes with ESC[0m.
SGR code
大意是:“\u001b”是一个特殊的转意字符,遵从一定的规则,可以用来设置文字或背景颜色。
上面代码中,前面的\u001b[31m用于设定SGR颜色,后面的\u001b[0m相当于一个封闭标签作为前面SGR颜色的作用范围的结束点标记。
知道原因了
那就来了解一下 Linux终端常用的一些颜色码。字体颜色码有:重置=0,黑色=30,红色=31,绿色=32,黄色=33,蓝色=34,洋红=35,青色=36,白色=37;背景色有:黑色底=40,红色底=41,绿色底=42,黄色底=43,蓝色底=44,紫色底=45,天蓝底=46,白色底=47。