Skip to content

Instantly share code, notes, and snippets.

@JohnArchieMckown
Last active August 29, 2015 14:07
Show Gist options
  • Save JohnArchieMckown/451ae7797e4e6d52c611 to your computer and use it in GitHub Desktop.
Save JohnArchieMckown/451ae7797e4e6d52c611 to your computer and use it in GitHub Desktop.
Shell function for BASH or Bourne shell which partially emulates the GNU/Linux which command output.
#
# This shell function will return with a 0 if the specified program is on the ${PATH}.
# and will echo the full path name, including the executable. If it is not on
# the ${PATH}, it will return with a code of 1, but no output. The return value saves
# the necessity of testing to see if anything was returned. A "conditional execution"
# might be something like:
# fullname=$(which someprog) && ${fullname}
# Not that you'd actually do the above. Perhaps something more like:
# fullname=$(which someprog) && {
# while read i;do ${fullname} $i;done <some.input
# }
# The above only does the resolution of "fullname" once and conditionally executes the loop if
# "someprog" is on the ${PATH}. Just a stupid thought.
which () {
test -z ${PATH} && return 1;
echo ${PATH} | tr ':' '\n' | while read path; do
test -e ${path}/$1 && { echo ${path}/$1; return 0; }
done
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment