Created
September 9, 2021 07:42
-
-
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
This file contains 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
#!/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