12

I am trying to get my regex highlighted on mac using terminal. I have put this is my .profile:

export GREP_COLOR='1;30;40'

Nothing changes, so I added the same to my bash.profile still nothing, restarted the machine nothing. When I am:

egrep --color=auto ".*\W blah blah "

then it lights up some yellow color but I wondered if I had done something wrong in my .profile or bash_profile.

Any help would be good: snow leopard, with just plain old terminal :-)

  • 2
    Isn't GREP_COLOR deprecated? Anyways grep --color should highlight matches in the default color, without any need for environment variables. – jw013 Mar 23 '12 at 0:07
  • That's only half true. GREP_COLOR allows you to customize the actual colors, whereas --color only enables/disables whatever colors are configured by default. And I THINK GREP_COLORS (plural) is what is deprecated. That's why my coloring form years ago disappeared without me noticing. – Sridhar Sarnobat Dec 4 at 3:20
22
+50

This worked on my snow leopard machine. Try just copying and pasting the text in your terminal and seeing if grep shows the desired behavior. If it does, then the problem is you're not adding it to the right dotfile. What shell are you using? The dotfile you should add it to depends on the shell you use.

export GREP_OPTIONS='--color=auto'
export GREP_COLOR='1;30;40'

Also, try running printenv to see what environmental variables you already have set and see if any grep options are already.

  • excellent!!! thank you!! much better than setting up an alias! – cwd Apr 26 '12 at 13:44
  • 1
    Weird. I was going to vote this answer up, but it didn't work for me using Terminal on Snow Leopard. It turned the matched text into all-black boxes. – Old Pro Apr 27 '12 at 4:36
  • 2
    @OldPro - I would recommend actually using export GREP_COLOR='1;35;40' because 35 is cyan and 30 is gray- 40 is black background, so it looks dark. But yes it works! :) – cwd Apr 27 '12 at 18:39
  • My terminal has become a lot more beautiful. I know true beauty is on the inside but I'm superficial. – Sridhar Sarnobat Dec 4 at 3:57
1

I don't think Snow Leopard's default setting is to use color for grep. I've had success on my OS X box with adding a line like alias grep=grep --color=always in my .bashrc to ensure consistent highlighting.

  • 6
    --color=always can be a problem if grep's output is piped to another command. --color=auto will emit the control characters for only when the output is directly displayed on the terminal – Gowtham Mar 23 '12 at 7:23
1

It's not bash.profile nor bash_profile, it's .bash_profile.

As for the colors on your Mac, just put this in your ~/.bash_profile:

export GREP_OPTIONS='--color=auto'
export GREP_COLOR='1;30;40'

Be sure to use .bash_profile in your home directory (~/).

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.