Last active
June 13, 2025 19:50
-
-
Save dmyersturnbull/407b85342850cec22816658b8224ac6e to your computer and use it in GitHub Desktop.
Run GNU programs installed as Keg-only Brew packages: `gnu <command> [<args ...>]`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because the built-in macOS programs are trash.
β Bad
emotional state: π
emotional state: π‘
β Good!
emotional state: π