Coloring Grep to easier research

his is an example of a search with grepwith a coloured output:


To get this result open a terminal and use your favourite editor to change the file .bashrc in your home directoy, in this example I’ll use vim

$ vim ~/.bashrc

And add these 2 lines:

alias grep="grep --color=auto"
export GREP_COLORS='0;31'

Save and try to use the grep command. The result should be the same of the former screenshot and the following:

Let’s see how to change the colors. The colors are defined in the string:


The value “31” stands for red font as defined by the color table of the shell:

Color	Foreground	Background
Black	    30	               40
Red	    31	               41
Green	    32	               42
Yellow	    33	               43
Blue	    34	               44
Magenta	    35	               45
Cyan	    36	               46
White	    37	               47

If we would like to see the text color in green, the value that we should use will be “32”.
Let’s see an example with the green color as background:


Exporting this variable:

export GREP_COLORS='0;30;42'

where “30” is used to define the color of the foreground, in this case black, and “42” is the background, the color green.

And if you are on a different computer and you want to just have 1 colored output without changing the .bashrc file ?
No problem you can use grep with the option --color=auto to get the output with the default color that usually is a red foreground:


This option surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS. WHEN is never, always, or auto.

To see the various combinations of Foregrounds and Backgrounds just refer to THIS page.