Differences between revisions 3 and 4
Revision 3 as of 2007-05-05 18:44:23
Size: 1394
Editor: S0106000d88bbefa2
Comment:
Revision 4 as of 2007-05-13 11:58:24
Size: 1500
Editor: BetterWorld
Comment:
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
If these builtins are not available (because you're in a Bourne shell, or whatever), then you may have to rely on the external command {{{which}}} (which is often a csh script, although sometimes a compiled binary). Unfortunately, {{{which}}} does ''not'' set a useful exit code -- and it doesn't even write errors to stderr! Therefore, one must parse its output. If these builtins are not available (because you're in a Bourne shell, or whatever), then you may have to rely on the external command {{{which}}} (which is often a csh script, although sometimes a compiled binary). Unfortunately, {{{which}}} does ''not'' set a useful exit code (on any system but GNU/Linux) -- and it doesn't even write errors to stderr! Therefore, one must parse its output.
Line 28: Line 28:
# Another easy way in gnu:
if [ -z "$(which qwerty)" ]; then
# Another easy way that works only on gnu:
if ! which qwerty 2>/dev/null; then
Line 35: Line 35:
(Also note that its output is ''not'' consistent across platforms. On HP-UX, for example, it prints {{{no qwerty in /path /path /path ...}}}; on OpenBSD, it prints {{{qwerty: Command not found.}}}; and on GNU/Linux, it prints nothing at all.) (Also note that its output is ''not'' consistent across platforms. On HP-UX, for example, it prints {{{no qwerty in /path /path /path ...}}}; on OpenBSD, it prints {{{qwerty: Command not found.}}}; and on Debian and Suse, it prints nothing at all. On Gentoo, it actually prints something to stderr.)

Anchor(faq81)

How can I determine whether a command exists anywhere in my PATH?

In BASH, there are a couple builtins that are suitable for this purpose: hash and type. Here's an example using hash:

if hash qwerty 2>/dev/null; then
  echo qwerty exists
else
  echo qwerty does not exist
fi

If these builtins are not available (because you're in a Bourne shell, or whatever), then you may have to rely on the external command which (which is often a csh script, although sometimes a compiled binary). Unfortunately, which does not set a useful exit code (on any system but GNU/Linux) -- and it doesn't even write errors to stderr! Therefore, one must parse its output.

# Last resort -- using which(1)
x=$(LC_ALL=C which qwerty 2>&1)
case "$x" in
  no\ *\ in\ *)           echo qwerty does not exist;;
  *Command\ not\ found.)  echo qwerty does not exist;;
  '')                     echo qwerty does not exist;;
  *)                      echo qwerty exists;;
esac

# Another easy way that works only on gnu:
if ! which qwerty 2>/dev/null; then
  echo "$0: install qwerty first"
  exit 1
fi

(Also note that its output is not consistent across platforms. On HP-UX, for example, it prints no qwerty in /path /path /path ...; on OpenBSD, it prints qwerty: Command not found.; and on Debian and Suse, it prints nothing at all. On Gentoo, it actually prints something to stderr.)

BashFAQ/081 (last edited 2023-05-22 10:02:19 by emanuele6)