Skip to content

Instantly share code, notes, and snippets.

@chrisranderson
Created July 12, 2023 22:54
Show Gist options
  • Select an option

  • Save chrisranderson/481e177f926d348a587ca6cadfc76b40 to your computer and use it in GitHub Desktop.

Select an option

Save chrisranderson/481e177f926d348a587ca6cadfc76b40 to your computer and use it in GitHub Desktop.
which all
function which_all() {
# Like `which`, but prints each location it finds
# on $PATH, not just the first one. zsh only.
#
# Example:
# > which_all conda
# /opt/homebrew/Caskroom/miniconda/base/bin/conda
# /opt/homebrew/Caskroom/miniconda/base/condabin/conda
# /opt/homebrew/bin/conda
typeset -A which_results
query=$1
for path_entry in ${(s/:/)PATH}; do
query_path="${path_entry}/${query}"
if [ -x "${query_path}" ]; then
# We are treating this associative array
# as a set; we only need the keys.
which_results[${query_path}]="throwaway_value"
fi
done
which_results_keys=("${(@k)which_results}")
for which_result in "${which_results_keys[@]}"; do
echo "${which_result}"
done | sort
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment