grep: highlighting matches in color
Posted by JoshTriplett on Mon 6 Nov 2006 at 09:48
Many Debian users use grep regularly. But did you know that grep can highlight the text it matches in color?
Much like ls, grep supports the use of color in its output, controlled by a --color option. By default, grep outputs matching lines, but does not indicate what portion of the line matched. With --color=auto, grep will highlight the matching portion of the line in color when it outputs to a terminal, while avoiding the use of any potentially-harmful terminal control sequences when standard output goes somewhere else (like a file or pipe). If you like the colorful grep, you can simply
export GREP_OPTIONS='--color=auto' in your shell startup script.
grep's default highlight uses a red color. You can change this color by setting the
GREP_COLOR environment variable to a different escape sequence fragment. I use
export GREP_COLOR='1;32', which produces a bright green.