How can I grep with color in Mac OS X's terminal?

28,138

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'
Share:
28,138

Related videos on Youtube

cwd
Author by

cwd

Updated on September 18, 2022

Comments

  • cwd
    cwd over 1 year

    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?

  • Breno Macena
    Breno Macena over 9 years
    Didn't work for me. :(
  • Mike Nguyen
    Mike Nguyen almost 9 years
    Did you restart your terminal ?
  • joelittlejohn
    joelittlejohn over 8 years
    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"
  • rfabbri
    rfabbri almost 8 years
    @joelittlejohn doesn't work when using grep in pipes with xargs, eg: find /usr/share -name '*.txt' |xargs grep testing
  • rcreswick
    rcreswick over 7 years
    @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).
  • joelittlejohn
    joelittlejohn over 7 years
    @rfabbri I guess that's one to take up with the grep team who deprecated this :)
  • Akavall
    Akavall over 7 years
    For some reason for me I did not see an effect after I opened a new terminal, but source ~/.bashrc did it.
  • Sampo
    Sampo over 6 years
    --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.
  • cde
    cde about 6 years
    @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