My GNU grep under Mac will not highlight the matched text with colors (See UPDATE, GREP_OPTIONS
not working):
whereas Under Linux everything is working out of the box:
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:
which is under iterm2, and the following is under xterm:
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 agrep
alias. Another solution would be to add a short script namedgrep
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.Since the default shell is
bash
for Ubuntu, the output from the commands below producesabc
with the letterb
in red. This happens becausegrep
is aliased togrep --color=auto
.Below is sample output from an Ubuntu Terminal window, where the shell is
![](https://i.sstatic.net/65alVLPB.png)
bash
.Ubuntu does not have
zsh
by default. I installed by entering the following.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 commandzsh
, I again entered the following.Below is example output from an Ubuntu Terminal window, where the shell is
![](https://i.sstatic.net/tiu1X1yf.png)
zsh
.This time the letter
b
was not colored red. So, the behavior is the same as macOS when usingzsh
, 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 ininfo grep
forgrep (GNU grep) 3.11
installed using Homebrew. If your macOS does not haveinfo
, then see this answer.The above does give me the following idea for how to support
GREP_OPTIONS
for your version ofgrep
installed in macOS. Make the following changes to macOS.grep
in the/usr/local/bin
directory. The script file should contain the lines given below. If desired, you can omit--devices=skip
option./usr/local/bin
is first in thePATH
variable. This should be the default. Below is my default path when testing under macOS Monterey.GREP_OPTIONS
variable. Below is an example.Versions Installed in Ubuntu 24.04 LTS