Skip to content

Instantly share code, notes, and snippets.

@dmyersturnbull
Last active June 13, 2025 19:50
Show Gist options
  • Save dmyersturnbull/407b85342850cec22816658b8224ac6e to your computer and use it in GitHub Desktop.
Save dmyersturnbull/407b85342850cec22816658b8224ac6e to your computer and use it in GitHub Desktop.
Run GNU programs installed as Keg-only Brew packages: `gnu <command> [<args ...>]`.
# Runs an executable in a Keg-only Brew package that's not in PATH.
# Finds an executable named `$1` or `g$1` in formula `$1`, `g$1`, `coreutils`, or `findutils`.
# Note: Always searches in that order. This is significant for commands starting with `g`.
# Usage: gnu <command> [<args ...>]
gnu() {
if (( $# == 0 )); then
printf >&2 "\e[0;1;31mUsage: %s <command> [args...]\e[0m\n" "${FUNCNAME[0]}"
return 2
fi
if ! command -v brew > /dev/null 2> /dev/null; then
printf >&2 "\e[0;1;31mBrew is not installed.\e[0m\n"
return 127
fi
local _opt="$HOMEBREW_PREFIX"/opt
_paths=(
"$_opt/$1/bin/$1"
"$_opt/$1/bin/g$1"
"$_opt/g$1/bin/g$1"
"$_opt/coreutils/bin/$1"
"$_opt/coreutils/bin/g$1"
"$_opt/findutils/bin/$1"
"$_opt/findutils/bin/g$1"
)
local _path
for _path in "${_paths[@]}"; do
if [[ -e "$_path" ]]; then
"$_path" "${@:2}" && return 0 || return $?
fi
done
printf >&2 "\e[1;31mExecutable '%s' not found.\e[0m\n\e[0;31mChecked locations" "$1"
printf >&2 " for formulas \e[1m%s\e[0;31m and \e[1mcoreutils\e[0;31m:\e[0m\n" "$1"
printf >&2 "\e[31m ⁃ %b\n" "${_paths[@]}"
printf >&2 "\e[31m%s\e[0m\n" "$(brew --prefix "$1" 2>&1)"
return 1
}
@dmyersturnbull
Copy link
Author

dmyersturnbull commented Jun 13, 2025

Because the built-in macOS programs are trash.

❌ Bad

$ find --version
find: illegal option -- -
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
       find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]

emotional state: 😠

$ brew --prefix find
brew --prefix find
Error: No available formula with the name "find". Did you mean ffind, zfind or fend?

emotional state: 😑


βœ… Good!

$ gnu find --version
find (GNU findutils) 4.10.0
Packaged by Homebrew
Copyright (C) 2024 Free Software Foundation, Inc.
[...]

emotional state: πŸ˜‡

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