Skip to content

Instantly share code, notes, and snippets.

@bittner
Last active December 11, 2021 20:05
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bittner/5436f3dc011d43ab7551 to your computer and use it in GitHub Desktop.
Save bittner/5436f3dc011d43ab7551 to your computer and use it in GitHub Desktop.
Cross-OS compatibility with GNU tools (readlink, sed, zcat, zless, zdiff, ...) for Mac OS X towards Linux
# DESCRIPTION OF PROBLEM: Implementations of sed, readlink, zcat, etc. are different on OS X and Linux.
# NOTE: Put this on top of your script using sed, readlink, zcat, etc. that should work alike on Mac OS X.
# cross-OS compatibility (greadlink, gsed, zcat are GNU implementations for OS X)
[[ `uname` == 'Darwin' ]] && {
which greadlink gsed gzcat > /dev/null && {
unalias readlink sed zcat
alias readlink=greadlink sed=gsed zcat=gzcat
} || {
echo 'ERROR: GNU utils required for Mac. You may use homebrew to install them: brew install coreutils gnu-sed'
exit 1
}
}
# NOTE: Now all uses of `sed`, `readlink`, `zcat`, etc. will refer to their GNU implementation in your script below.
# NOTE: In order to use the original implementation for Mac OS X again you'd have to do `unalias ...` (as in line 7 above).
@d1vyank
Copy link

d1vyank commented Nov 15, 2017

Note: If you are using this within a script you will also need to add: 'shopt -s expand_aliases' to the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment