2

My GNU grep under Mac will not highlight the matched text with colors (See UPDATE, GREP_OPTIONS not working):

enter image description here

whereas Under Linux everything is working out of the box:

enter image description here

and my Mac setting is (almost) the same as Linux, with even a newer version of grep:

$ env | grep GREP | wc
      0       0       0

$ type grep
grep is hashed (/opt/local/libexec/gnubin/grep)

$ grep -V | head -1
grep (GNU grep) 3.11

$ sw_vers
ProductName:            macOS
ProductVersion:         14.4.1
BuildVersion:           23E224

UPDATE:

Thanks for your input Allan, somehow GREP_OPTIONS is not working for me:

enter image description here

which is under iterm2, and the following is under xterm:

enter image description here

Why the GREP_OPTIONS is not working? From man under my Mac:

ENVIRONMENT
GREP_OPTIONS May be used to specify default options that will be placed at the beginning of the argument list.

5
  • The environment variable works for me in Terminal. Does coloring in xterm work if you use grep --color=always ...?
    – nohillside
    Commented May 17 at 15:31
  • There is no mention of GREP_OPTIONS existing in the man page for grep (GNU grep) 3.11. At least not for the one installed using Homebrew. See man /usr/local/opt/grep/libexec/gnuman/man1/grep.1 or just enter man /usr/local/opt/grep/libexec/gnuman/man1/grep.1 | grep GREP. Commented May 17 at 17:14
  • @DavidAnderson The OP is talking about Macs and iTerm2.
    – nohillside
    Commented May 17 at 18:10
  • @nohillside: Are you referring to my previous comment? If so, I was talking about Macs. Everything posted in the comment was taken from the install of macOS Monterey 12.5 in a virtual machine on a 2013 iMac. As I already stated, grep was installed using Homebrew. Although not explicitly stated by the OP, I assume the OP also used Homebrew. I am not sure why you referred to iTerm2, since I believe the terminal application should not make any difference. Commented May 17 at 20:39
  • @nohillside: I also now realize that Alan posted the same to his answer one hour before I posted my comment. Commented May 17 at 20:39

2 Answers 2

1

One possible reason for the differing behavior would be you have a grep alias defined in Linux and not in macOS. One solution under macOS would be to add a grep alias. Another solution would be to add a short script named grep to /usr/local/bin. Explanations are given below.


I installed Ubuntu 24.04 LTS (Linux) in a virtual machine. When a new user account is created, the home folder includes a default ~/.bashrc file. The following lines are included in this file.

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'

Since the default shell is bash for Ubuntu, the output from the commands below produces abc with the letter b in red. This happens because grep is aliased to grep --color=auto.

echo abc | grep b

Below is sample output from an Ubuntu Terminal window, where the shell is bash.

Ubuntu does not have zsh by default. I installed by entering the following.

apt install zsh

After entering the command zsh for the first time, I was prompted and chose to populate my ~/.zshrc file with the configuration recommended by the system administrator. This ~/.zshrc file contains no aliases. After entering the command zsh, I again entered the following.

echo abc | grep b

Below is example output from an Ubuntu Terminal window, where the shell is zsh.

This time the letter b was not colored red. So, the behavior is the same as macOS when using zsh, which is the default shell for macOS.


Environment Variables

The command info grep often produces more details than the man page. In this case, the following exists in info grep for grep (GNU grep) 3.11 installed using Homebrew. If your macOS does not have info, then see this answer.

   The ‘GREP_OPTIONS’ environment variable of ‘grep’ 2.20 and earlier is
no longer supported, as it caused problems when writing portable
scripts.  To make arbitrary changes to how ‘grep’ works, you can use an
alias or script instead.  For example, if ‘grep’ is in the directory
‘/usr/bin’ you can prepend ‘$HOME/bin’ to your ‘PATH’ and create an
executable script ‘$HOME/bin/grep’ containing the following:

     #! /bin/sh
     export PATH=/usr/bin
     exec grep --color=auto --devices=skip "$@"

The above does give me the following idea for how to support GREP_OPTIONS for your version of grep installed in macOS. Make the following changes to macOS.

  • Create a script file named grep in the /usr/local/bin directory. The script file should contain the lines given below. If desired, you can omit --devices=skip option.
    #! /bin/sh
    export PATH=/usr/local/opt/grep/libexec/gnubin
    eval set -- "$GREP_OPTIONS" "$@"
    exec grep --devices=skip "$@"
    
  • Make sure the script is executable by enter the following command.
    chmod +x /usr/local/bin/grep
    
  • Make sure /usr/local/bin is first in the PATH variable. This should be the default. Below is my default path when testing under macOS Monterey.
    % echo PATH
    /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
    
  • Export the desired GREP_OPTIONS variable. Below is an example.
    export GREP_OPTIONS='--color=auto'
    

Versions Installed in Ubuntu 24.04 LTS

GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
zsh 5.9 (x86_64-pc-ubuntu-gnu)
grep (GNU grep) 3.11
2
  • Indeed David, the grep under Linux is indeed an alias. Do you know how to get GREP_OPTIONS working without using alias pls?
    – xpt
    Commented May 17 at 14:07
  • 1
    xpt: I updated my answer with respect to your comment. This is closest solution I can determine for getting your version of grep to work with the GREP_OPTIONS variable. Commented May 18 at 0:47
5

With the BSD version of grep, you need to specify the color option if you want colorized output:

grep --color=always

Output:

enter image description here

However, typing that option for every instance of grep can become quite tiresome fairly quickly. Therefore, you should consider making grep an alias of the command with the color option specified or specify it in the GREP_OPTIONS environmental variable (my preferred method).

Alias:

alias grep="grep --color=always"

Environment Variable

export GREP_OPTIONS='--color=always'

enter image description here

You can add either line to .zshrc or .zprofile. If you happen to be using Bash, it would be in either .bashrc or .bash_profile

I installed GNU grep (ggrep) for testing purposes. I found the following:

  • per the man page (man ggrep) GREP_OPTIONS environment variable isn't used.

  • the command line option --color=always works as expected

    enter image description here

  • To make it permanant, you have to use the alias function.

  • This works in both Zsh and Bash shells

    enter image description here

3
  • Thanks for the input, my grep under Mac is not BSD based but GNU based. See my type command in OP, and somehow, GREP_OPTIONS is not working for me either, see updated OP please.
    – xpt
    Commented May 17 at 13:56
  • 2
    alias grep="grep --color=always" is the way to go. Works for both BSD grep and GNU grep
    – Volsk
    Commented May 17 at 14:47
  • Thank you for all the explanations, +1
    – xpt
    Commented May 17 at 18:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .