Targeted Use of GNU Tools on macOS using direnv
Working with bash scripts on macOS often leads to an interesting predicament. MacOS ships with the BSD variants of sed
, date
, base64
and others that diverge just enough from their GNU counterparts to cause incompatibilities.
Take sed
for example:
# GNU (Linux) version - works fine
sed -i 's/foo/bar/' file.txt
# macOS version - fails
sed -i 's/foo/bar/' file.txt
# Error: sed: 1: "file.txt": undefined label 'ile.txt'
# macOS version - requires an extension
sed -i '' 's/foo/bar/' file.txt
There are similar stories for date