Highlighting “grep” matches

“grep” is a useful Linux tool allowing the user to print lines matching a specific search pattern. Most popular uses is by “piping” the output of one command to “grep” (such as cat /path/to/file | grep findme), or by directly looking for a pattern inside one or more file (e.g. grep findme /path/to/*.txt).

This would print lines that match the search string / expression. However, how to colorize the matching patterns? Simple! Use the --color=always flag, i.e.:

grep --color=always -i findme /path/to/*.txt

Next, if you’re doing lots of “grep” work and don’t want to keep specifying the color flag, you can opt for the following:

Add it to the GREP_OPTIONS environment variable

export GREP_OPTIONS=’--color=always’

Or, if you’re using bash as your shell, add it to /etc/bashrc. Simply edit that file using your preferred editor (e.g. vim, nano, etc.), and add the above export command at the very end of the file. When done, save and exite the editor, then issue the following command, so that the changes take effect: source /etc/bashrc

Leave a Reply