38

I recently found that on Mac OS X I can set this up in my shell ~/.profile so that when I use grep it will print the matches in color (white one red).

alias grep='GREP_COLOR="1;37;41" LANG=C grep --color=auto'

But setting up an alias seems like kind of a hack way to do this. Previously I had tried with no luck:

export GREP_COLOR=always           # works fine in Linux

And then I also tried:

export GREP_COLOR="1;37;41"

Is there a better way to do this than setting up an alias?

57

Per Grep_color on mac as suggested by @lupincho, this seems to work fine and does not use an alias:

export GREP_OPTIONS='--color=always'
export GREP_COLOR='1;35;40'
  • Didn't work for me. :( – trusktr Nov 25 '14 at 23:04
  • 13
    Have a +1 for this as a good solution, however setting GREP_OPTIONS leads to grep: warning: GREP_OPTIONS is deprecated; please use an alias or script. Something like this is now preferred: alias grep="`which grep` --color=always" – joelittlejohn Nov 6 '15 at 12:06
  • 2
    @joelittlejohn doesn't work when using grep in pipes with xargs, eg: find /usr/share -name '*.txt' |xargs grep testing – rfabbri May 27 '16 at 15:29
  • 1
    @rfabbri I guess that's one to take up with the grep team who deprecated this :) – joelittlejohn Oct 5 '16 at 12:16
  • 1
    --color=alwaysis dangerous. Scripts run from the shell will also inherit the option and cause very mysterious failures when grep starts injecting color codes to output inside scripts. --color=auto doesn't cause this problem. – Sampo Sep 6 '17 at 13:24

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.