Hide

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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

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.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

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

Alternatives

There are 11 alternatives - 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 161 weeks and 3 days ago

@zwettler

try this instead

command -v /bin/ls

also try

help command

for all the options on `command`

Comment by unixmonkey365 161 weeks and 1 day 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.

Comment by zwettler 161 weeks 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 81 weeks and 5 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts