GNU grep, how to enable colouring

command linesonoma

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.

Best Answer

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