What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Determine if a command is in your $PATH using POSIX

Terminal - Determine if a command is in your $PATH using POSIX
command -v bash
2011-09-26 10:17:41
User: atoponce
Functions: command
Determine if a command is in your $PATH using POSIX

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).


There is 1 alternative - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

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'

Comment by zwettler 337 weeks and 4 days ago


try this instead

command -v /bin/ls

also try

help command

for all the options on `command`

Comment by unixmonkey365 337 weeks and 1 day ago


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.

Comment by zwettler 337 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.

Comment by khayyam 257 weeks and 6 days ago

Your point of view

You must be signed in to comment.