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
xterm
work if you usegrep --color=always ...
?GREP_OPTIONS
existing in the man page forgrep (GNU grep) 3.11
. At least not for the one installed using Homebrew. Seeman /usr/local/opt/grep/libexec/gnuman/man1/grep.1
or just enterman /usr/local/opt/grep/libexec/gnuman/man1/grep.1 | grep GREP
.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.