When I grep something, the result is always in red. I know that the command grep --color
prints the result in color, which by default is red. Can I change the color?
Command Line – How to Modify Grep Color
command linegrep
command linegrep
When I grep something, the result is always in red. I know that the command grep --color
prints the result in color, which by default is red. Can I change the color?
Best Answer
You can change the highlight color of
grep
by using an environment variable,GREP_COLORS
, which you can set like this:Numeric options
The numbers can style text, change the foreground color or the background color, or change fonts.
The starting conditions for all
GREP_COLORS
options are the terminal's default text style, font, and colors. Resetting any of these will revert to the terminal's defaults, not any ofgrep
's defaults.Legend
ᴀ;ʙ;…
—;
separates numeric options that you want to combine (e.g., bold yellow-on-black text combines options1
,33
, and40
into1;33;40
)+ᴇꜰꜰᴇᴄᴛ
— ᴇꜰꜰᴇᴄᴛ gets turned on when you use that numeric option-ᴇꜰꜰᴇᴄᴛ
— ᴇꜰꜰᴇᴄᴛ gets turned off when you use that numeric optioncolor
,green
,cyan
, andgrey
— in the "Text styling" section, these refer to the foreground colorcolors
— in the "Text styling" section, this refers to both the foreground color and the background colorthis color
— in the "Foreground colors" section, this refers to the foreground color; in the "Background colors" section, this refers to the background colorText styling
An empty string or
0
resets all text styling and resets both colors to the defaults but does not reset the font to the default.Foreground colors
Background colors
Note that the non-GUI TTY doesn't provide a brighter background color series.
256-color chart
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 supports48;5;ɴ
— replace ɴ with the value of a particular color in the chart above to change the background color to the closest color the terminal supportsFor example,
38;5;214;48;5;30
will set the foreground to color214
and the background to color30
, 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
through20
are potential alternate fonts (20
usually means a ??????? font in the rare terminals that support it). Only10
and12
seem to exist by default in the non-GUI TTY, and none exist in GNOME Terminal orxterm
.grep
doesn't properly switch fonts back before exiting, so runreset
if you get stuck in an unwanted font aftergrep
returns to the shell.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