修改grep的颜色

25 command-line grep

当我 grep 某些东西时,结果总是红色的。我知道该命令grep --color以颜色打印结果,默认情况下为红色。我可以改变颜色吗?

M. *_*rra 25

您可以grep使用环境变量更改 的突出显示颜色GREP_COLORS,您可以这样设置:

export GREP_COLORS='ms=01;31'
Run Code Online (Sandbox Code Playgroud)

数字选项

数字可以设置文本样式、更改前景色或背景色,或更改字体。

所有GREP_COLORS选项的起始条件是终端的默认文本样式、字体和颜色。重置其中任何一个都将恢复到终端的默认值,而不是任何grep的默认值。

传奇

  • ?;?;-;要结合分离数字选项(例如,大胆的黄上黑色文本合并选项133401;33;40
  • +??????—— ??????当您使用该数字选项时被打开
  • -??????—— ??????当您使用该数字选项时被关闭
  • colorgreencyangrey— 在“文本样式”部分中,这些指的是前景色
  • colors — 在“文本样式”部分,这指的是前景色和背景色
  • this color——在“前景色”部分,这里指的是前景色;在“背景颜色”部分,这是指背景颜色

文字样式

空字符串或0重置所有文本样式并将两种颜色重置为默认值,但不会将字体重置为默认值。

???????????????????????????????????????????????????????????????????????????????????
? ### ? GNOME Terminal          ? xterm                   ? non-GUI TTY           ?
???????????????????????????????????????????????????????????????????????????????????
?     ? «reset style+colors»    ? «reset style+colors»    ? «reset style+colors»  ?
?   0 ? «reset style+colors»    ? «reset style+colors»    ? «reset style+colors»  ?
???????????????????????????????????????????????????????????????????????????????????
?   1 ? +bold, +brighter color  ? +bold, +brighter color  ? +brighter color,      ?
?     ?                         ?                         ?   -forced grey        ?
?   2 ? +fainter color          ? +fainter color          ? +forced grey          ?
?   3 ? +italic                 ? +italic                 ? +forced green         ?
?     ?                         ?                         ?   ? overrides 2 and 4 ?
?   4 ? +underline              ? +underline              ? +forced cyan          ?
?     ?                         ?                         ?   ? overrides 2       ?
?   5 ? «no effect»             ? +blink                  ? «no effect»           ?
?   7 ? +invert colors          ? +invert colors          ? +invert colors        ?
?   8 ? +invisible              ? +invisible              ? «no effect»           ?
?     ?                         ?   ? underline appears   ?                       ?
?   9 ? +strikethrough          ? +strikethrough          ? «no effect»           ?
?????????????????????????????????                         ?????????????????????????
?  21 ? -bold, -brighter color, ? +double underline       ? -brighter color,      ?
?     ?   -fainter color        ???????????????????????????   -forced grey        ?
?  22 ? -bold, -brighter color, ? -bold, -brighter color, ? -brighter color,      ?
?     ?   -fainter color        ?   -fainter color        ?   -forced grey        ?
?  23 ? -italic                 ? -italic                 ? -forced green         ?
?  24 ? -underline              ? -underline,             ? -forced cyan          ?
?     ?                         ?   -double underline     ?                       ?
?  25 ? «no effect»             ? -blink                  ? «no effect»           ?
?  27 ? -invert colors          ? -invert colors          ? -invert colors        ?
?  28 ? -invisible              ? -invisible              ? «no effect»           ?
?  29 ? -strikethrough          ? -strikethrough          ? «no effect»           ?
???????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

前景色

???????????????????????????????????????????????????????????????????????????????????
? ### ? GNOME Terminal          ? xterm                   ? non-GUI TTY           ?
???????????????????????????????????????????????????????????????????????????????????
?  39 ? «reset this color»      ? «reset this color»      ? «reset this color»    ?
???????????????????????????????????????????????????????????????????????????????????
?  30 ? very dark grey          ? black                   ? black                 ?
?  31 ? dull red                ? red                     ? light red             ?
?  32 ? dull green              ? light green             ? light green           ?
?  33 ? dull yellow             ? yellow                  ? yellow                ?
?  34 ? greyish blue            ? dark blue               ? sky blue              ?
?  35 ? dull purple             ? purple                  ? purple                ?
?  36 ? teal                    ? cyan                    ? cyan                  ?
?  37 ? light grey              ? light grey              ? light grey            ?
???????????????????????????????????????????????????????????????????????????????????
?  90 ? dark grey               ? dull grey               ? dull grey             ?
?  91 ? red                     ? bright red              ? bright red            ?
?  92 ? lime green              ? bright green            ? bright green          ?
?  93 ? yellow                  ? bright yellow           ? pure yellow           ?
?  94 ? light greyish blue      ? dull blue               ? deep blue             ?
?  95 ? light purple            ? magenta                 ? magenta               ?
?  96 ? cyan                    ? bright cyan             ? bright cyan           ?
?  97 ? off white               ? white                   ? white                 ?
???????????????????????????????????????????????????????????????????????????????????
? 38;2;?;?;? ? replace ?, ?, and ? with RGB values from 0 to 255                  ?
?            ?   for closest supported color (non-GUI TTY has only 16 colors!)    ?
? 38;5;?     ? replace ? with value from 256-color chart below                    ?
?            ?   for closest supported color (non-GUI TTY has only 16 colors!)    ?
???????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

背景颜色

请注意,非 GUI TTY 不提供更亮的背景颜色系列。

???????????????????????????????????????????????????????????????????????????????????
? ### ? GNOME Terminal          ? xterm                   ? non-GUI TTY           ?
???????????????????????????????????????????????????????????????????????????????????
?  49 ? «reset this color»      ? «reset this color»      ? «reset this color»    ?
???????????????????????????????????????????????????????????????????????????????????
?  40 ? very dark grey          ? black                   ? black                 ?
?  41 ? dull red                ? red                     ? light red             ?
?  42 ? dull green              ? light green             ? light green           ?
?  43 ? dull yellow             ? yellow                  ? yellow                ?
?  44 ? greyish blue            ? dark blue               ? sky blue              ?
?  45 ? dull purple             ? purple                  ? purple                ?
?  46 ? teal                    ? cyan                    ? cyan                  ?
?  47 ? light grey              ? light grey              ? light grey            ?
???????????????????????????????????????????????????????????????????????????????????
? 100 ? dark grey               ? dull grey               ? black                 ?
? 101 ? red                     ? bright red              ? light red             ?
? 102 ? lime green              ? bright green            ? light green           ?
? 103 ? yellow                  ? bright yellow           ? yellow                ?
? 104 ? light greyish blue      ? dull blue               ? sky blue              ?
? 105 ? light purple            ? magenta                 ? purple                ?
? 106 ? cyan                    ? bright cyan             ? cyan                  ?
? 107 ? off white               ? white                   ? light grey            ?
???????????????????????????????????????????????????????????????????????????????????
? 48;2;?;?;? ? replace ?, ?, and ? with RGB values from 0 to 255                  ?
?            ?   for closest supported color (non-GUI TTY has only 8 colors!)     ?
? 48;5;?     ? replace ? with value from 256-color chart below                    ?
?            ?   for closest supported color (non-GUI TTY has only 8 colors!)     ?
???????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

256色图表

256色图表

Above chart is a screenshot of content published in "ANSI escape code, 8-bit colors" on Wikipedia, authored by CMG Lee et al. on 24 February 2016 and later, and licensed by the authors under both the CC BY-SA 3.0 copyright license and the GFDL copyright license.

Colors from the 256-color chart above can be used as follows:

  • 38;5;? — replace ? with the value of a particular color in the chart above to change the foreground color to the closest color the terminal supports
  • 48;5;? — replace ? with the value of a particular color in the chart above to change the background color to the closest color the terminal supports

For example, 38;5;214;48;5;30 will set the foreground to color 214 and the background to color 30, giving an orange-on-teal result on terminals that support it.

Note that not all terminals support all 256 colors, so it's important to realize that the chosen color might not be used. Only the supported color closest to the one chosen will be used.

For example, the non-GUI TTY only supports the basic 16 colors for foreground and the basic 8 colors for background, so the closest colors that end up being used may not be what you expect. As an example, the orange-on-teal selection above (38;5;214;48;5;30) shows as yellow-on-black in the non-GUI TTY, since those are the closest supported colors.

Fonts

10 is the default font. 11 through 20 are potential alternate fonts (20 usually means a font in the rare terminals that support it). Only 10 and 12 seem to exist by default in the non-GUI TTY, and none exist in GNOME Terminal or xterm.

grep doesn't properly switch fonts back before exiting, so run reset if you get stuck in an unwanted font after grep returns to the shell.

???????????????????????????????????????????????????????????????????????????????????
? ### ? GNOME Terminal          ? xterm                   ? non-GUI TTY           ?
???????????????????????????????????????????????????????????????????????????????????
?  10 ? «no effect»             ? «no effect»             ? «reset font»          ?
?  11 ? «no effect»             ? «no effect»             ? -messed up font       ?
?  12 ? «no effect»             ? «no effect»             ? +messed up font       ?
???????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

"Numeric options" section sources:
        ? "ANSI escape code, SGR (Select Graphic Rendition) parameters" on Wikipedia
        ? Experimental verification on GNOME Terminal, xterm, and non-GUI-mode TTY on Ubuntu 16.04


设置<code>GREP_COLORS</code>后颜色变化的例子

The default value of GREP_COLORS is 'ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'

The meaning of every element accepted on GREP_COLORS can be checked at GNU.org's manual page.

For completion, and as pointed out by @damadam, you need to add the export to your .bashrc in order to save the changes.

Related:

Multicolored Grep

Use different colors for every another grep

  • 有 ** 多** 多种颜色和格式选项可用,试试 `&lt;&lt;&lt;hello GREP_COLORS='ms=5;104' grep '.*'` 一次!可以找到代码列表,例如 [here](http://leocharre.com/articles/setting-ls_colors-colors-of-directory-listings-in-bash-terminal/)(第 6 节)。 (2认同)
  • @ChaiT.Rex 谢谢,虽然对我来说问题更多是在文本的“256 色”部分没有提到这些选项,只是颜色表 (2认同)

dam*_*dam 6

除了@M的答案。Becerra,不要忘记在export里面添加这个~/.bashrc。你可以这样做:

nano ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

现在输入您的export

export GREP_COLORS='ms=01;33:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
Run Code Online (Sandbox Code Playgroud)

这将grep结果设置为黄色,并且我不需要export重新启动终端或计算机后重做。

  • 编辑原始答案以使其下次更好。 (5认同)