How can I configure grep so that I could highlight in color smarter? By using different colors?

Problem: grep --color defaults to using red to highlight matches, but it still difficult to read for consecutive runs. If I run grep three times, I would like the first run to use red, the second to use some other color (say, green), the third to use red again, and so on.

I read before there is a hack to swap the color in terminal use every another run, so that output will be clean and easier to read. But I forget what that is; is it zsh or bash-script, or some other shell?


You could add the following to your .bashrc file:

my_grep() {
    export GREP_USE_FIRST_COLOR=false
    export GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
    export GREP_USE_FIRST_COLOR=true
    export GREP_COLORS='ms=01;33:mc=01;33:sl=:cx=:fn=35:ln=32:bn=32:se=36'
  grep --color=auto "$@"
alias grep=my_grep

This will alter the match highlighting color of subsequent grep calls in an interactive shell between bold red (ms=01;31) and bold yellow (ms=01;33).

See the grep manual for more information on how to use GREP_COLORS to set the particular highlighting colors you like.


Inspired by answer of @Chriki

By using PROMPT_COMMAND as a hook and update the GREP_COLOR and git config for every prompt.

# .bashrc
# ...
# your alias, git prompt, git completetion, exports... etc
# ...
__grep_swap_color ()
  if [ -z "$GREP_COLOR" ]
    export GREP_COLOR='1;32'
    git config --global color.grep.match blue
    export GREP_COLOR=''
    git config --global color.grep.match red
# End of file
  • 1
    Thanks for letting me inspire you :-) Please let me point you to a few drawbacks of your solution: (1) You won’t get the color alteration whenever you execute another shell command between two grep runs or when you just hit Enter after a grep run; (2) Your solution is a bit expensive in that it runs quite a bit of code for every new prompt; (3) Your bash will complain on each prompt when PROMPT_COMMAND is not set because the prompt command will then start with a ;; (4) GREP_COLOR is deprecated.
    – Chriki
    Nov 19 '13 at 11:40

You are looking for a Simple way to colour alternate output lines in bash.

If the answer seems a bit confusing, follow this.

  1. You have to use a script that automatically does this for you. Open up "terminal". (In many Ubuntu variations and itself, you press Alt+Ctrl+T to invoke that.) I'm assuming you to be using Ubuntu.
  2. Use your favorite editor to open a text file for writing the script. I'm using 'nano'. Like this nano ~/grepc.sh. This opens a file called grepc.sh in my /home (~/).
  3. Paste this bash script into that.

    while read line
      echo -e '\e[1;31m'"$line"
      read line
      echo -e '\e[1;32m'"$line"
    echo -en '\e[0m'
  4. If you're in nano, pressing Ctrl+X will attempt to close nano, but before that it will ask if you want to save the file. Do so, by pressing y on your keyboard.

  5. Now you have to make this script executable. Type the command chmod +x ~/grepc.sh.
  6. That's it. Now use this with grep commands like this: ls ~/ | grep "test" | ./grepc.sh (Assuming the command is executed in /home).
  • I think you read my question wrong.
    – Dennis C
    Nov 19 '13 at 0:48

This is an adaptation of Kimvais’ solution (which Unrealist also used).  Create grepc.bash:

while read line
     echo "$line"
     read line && echo "$line" | sed $'s/\(\e\[\([0-9]+;\))*\)31m/\\132m/g'

This uses sed to change the escape sequence for red to the escape sequence for green in alternate lines of its input.  Use, for example, as

grep --color=always (whatever) (whatever) | (path to the script)/grepc.bash

It seems that grep --color actually uses color only if it’s writing to the terminal (window), and you need to use =always to get it to use color when it’s writing to a pipe.  And BTW, it’s conventional to put personal utilities like this into ~/bin, and then add that to your PATH, so you don’t need to specify it when you run the utility.

  • I was not asking every another line, but every another run
    – Dennis C
    Aug 14 '14 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.