Determine if a command is in your $PATH using POSIX

command -v bash
it is generally advised to avoid using which(1) whenever possible. which(1) is usually a csh(1) script, or sometimes a compiled binary. It's output is highly variable from operating system to operating system, so platform independent scripts could become quite complicated with the logic. On HP-UX 10.20, for example, it prints "no bash in /path /path /path ..."; on OpenBSD 4.1, it prints "bash: Command not found."; on Debian (3.1 through 5.0 at least) and SuSE, it prints nothing at all; on Red Hat 5.2, it prints "which: no bash in (/path:/path:...)"; on Red Hat 6.2, it writes the same message, but on standard error instead of standard output; and on Gentoo, it writes something on stderr. And given all these differences, it's still variable based on your shell. This is why POSIX is king. See http://mywiki.wooledge.org/BashFAQ/081 for more ways on avoiding which(1).
Sample Output
/bin/bash

4
By: atoponce
2011-09-26 10:17:41

What Others Think

there is still a problem with bash! if there is an alias with the same name it will give you the alias: command -v ls alias ls='ls --color=auto'
zwettler · 364 weeks and 4 days ago
@zwettler try this instead command -v /bin/ls also try help command for all the options on `command`
unixmonkey365 · 364 weeks and 2 days ago
@unixmonkey24656 true, that works. but you have to know the whole path of the file and you don't get all instances of it in your $PATH. if you just want to check this particular instance its probably the best.
zwettler · 364 weeks and 1 day ago
The question is why is it so "highly variable", I'd hazzard that its because bash doesn't have it as a builtin. In the above HP-UX and OpenBSD outputs the scripts are obviously calling bash, and *very* oddly this seems the case for the two versions of Redhat you cite. Gentoo's sys-apps/which is part of @system, and the carlo17 binary, is there because the default shell, bash, doesn't provide it. Most of this can be attributed to the expectation that bash is present and as it doesn't provide 'which' as a builtin and so needs a 'bash' script or external binary. A Secondary question is why if POSIX shells support 'command' did 'which' come into such prevelant use ... pdksh, zsh, mksh, have both 'command' and 'which'/'whence' as builtins for that same purpose, so some external command isn't required, but the same case can't be made for bash. So, really, this isn't a case of "POSIX is king" but an issue directly attributable to bash being poorly equiped, and all the kludges whipped up to work arround the issue.
khayyam · 285 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: