I think most of us realize that macOS isn't a Linux OS, but what that also means is that instead of shipping with the GNU flavor of command line tools, it ships with the FreeBSD flavor. As such, writing shell scripts which can work across both platforms can sometimes be challenging.
Homebrew can be used to install the GNU versions of tools onto your Mac, but they are all prefixed with "g" by default.
All commands have been installed with the prefix "g". If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH from your bashrc.
You can install most of the GNU flavored tools with:
brew install autoconf bash binutils coreutils diffutils ed findutils flex gawk \
gnu-indent gnu-sed gnu-tar gnu-which gpatch grep gzip less m4 make nano \
screen watch wdiff wget zip
Assuming you have a fairly standard Terminal/shell environment, and assuming that you want to use the GNU versions instead of the BSD versions for everything you've installed with Homebrew, you can append the following to your ~/.profile
file.
UPDATE (2022-06-16): I now have an Intel MacBook Pro and an Apple Silicon Mac Studio. Homebrew moved with the new CPU architecture, so the following is what I now use that works across both machines (my shell profile is shared across machines with Dropbox and symlinks).
BREW_BIN="/usr/local/bin/brew"
if [ -f "/opt/homebrew/bin/brew" ]; then
BREW_BIN="/opt/homebrew/bin/brew"
fi
if type "${BREW_BIN}" &> /dev/null; then
export BREW_PREFIX="$("${BREW_BIN}" --prefix)"
for bindir in "${BREW_PREFIX}/opt/"*"/libexec/gnubin"; do export PATH=$bindir:$PATH; done
for bindir in "${BREW_PREFIX}/opt/"*"/bin"; do export PATH=$bindir:$PATH; done
for mandir in "${BREW_PREFIX}/opt/"*"/libexec/gnuman"; do export MANPATH=$mandir:$MANPATH; done
for mandir in "${BREW_PREFIX}/opt/"*"/share/man/man1"; do export MANPATH=$mandir:$MANPATH; done
fi