54

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?

cwd
  • 17,668
  • 42
  • 121
  • 159
  • I don't see the point of your question. An alias is how you do this. That's what it's for. `export` the environment variable e.g. in `.bash_profile`, and define `alias grep='grep --color'`, and you're done. – Daniel Beck Apr 25 '12 at 18:05
  • 3
    check this: http://unix.stackexchange.com/questions/34790/grep-color-on-mac – lupincho Apr 25 '12 at 18:35
  • tl/dr: just do this instead: `export GREP_OPTIONS='--color=auto'` – MarkHu May 07 '21 at 00:22

2 Answers2

83

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'
Steve Brown
  • 883
  • 8
  • 7
  • Didn't work for me. :( – trusktr Nov 25 '14 at 23:04
  • Did you restart your terminal ? – Mike Nguyen Jul 16 '15 at 08:31
  • 15
    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 06 '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
  • @joelittlejohn's comment makes this a non-starter for me; it prints that warning on every grep command (so, many, many times on a find operation). – rcreswick Oct 04 '16 at 22:11
  • 1
    @rfabbri I guess that's one to take up with the grep team who deprecated this :) – joelittlejohn Oct 05 '16 at 12:16
  • 1
    For some reason for me I did not see an effect after I opened a new terminal, but `source ~/.bashrc` did it. – Akavall Dec 01 '16 at 18:14
  • 8
    `--color=always`is 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 06 '17 at 13:24
  • 3
    @joelittlejohn that is not true for the default grep in OSX. Even on High Sierra 10.13, it uses FREEBSD grep 2.5.1 which HAS NOT depreciated GREP_OPTIONS. That's only true in gnu/grep – cde Feb 03 '18 at 02:32
0

On macOS Catalina with grep (BSD grep) 2.5.1-FreeBSD I use the --colour argument when calling grep to activate highlighting.

grep --colour thon /usr/share/dict/words

I found this option when I ran 'man grep' after finding this thread. It seems odd the spelling of the argument is '--colour', and not '--color'. This command prints the results with text highlighted in red.

Tim Sonner
  • 31
  • 2