Skip to content

Instantly share code, notes, and snippets.

@TheLonelyGhost
Created September 9, 2021 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheLonelyGhost/e25d4cf6277be61cae563e5358fc4b0b to your computer and use it in GitHub Desktop.
Save TheLonelyGhost/e25d4cf6277be61cae563e5358fc4b0b to your computer and use it in GitHub Desktop.
list all the places in one's PATH where a command might be. More reliable than `whence` in a cross-platform setting
#!/usr/bin/env bash
set -euo pipefail
if [ $# -lt 1 ]; then
printf 'USAGE: list-paths <command>\n' 1>&2
exit 1
fi
bin="$1"
found=0
for item in ${PATH//:/ }; do
if [ ! -e "$item" ]; then continue; fi
if [ -e "$item/$bin" ]; then
printf '%s\n' "$item/$bin"
found=1
fi
# find "$item" -maxdepth 1 -mindepth 1 -name "${1-*}" -print
done
if [ $found -eq 0 ]; then
exit 127
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment