7 ms·
> * The POSIX-blessed way of finding an executable program is command -v, which is consequently built into most shells. Given the standard alternative, Adams sa
by kelnos 5y ago
> * The POSIX-blessed way of finding an executable program is command -v, which is consequently built into most shells. Given the standard alternative, Adams said, "surely no one competent would choose to have a package depend on `which` when a standard POSIX utility can do a better job".*
This feels a little tone-deaf to me. I've been using the *nix command-line and writing (and reading) shell scripts for 20+ years, and I'd never heard of "command -v" until now. Now that I know about it, I'll probably start trying to retrain my muscle memory to use it (though it requires 2x the number of keystrokes).
Despite its lack of true standardization, "which" (along with "type -p") has been the de-facto "standard" I've seen in shell scripts for figuring out if a command exists. It was a surprise to me to learn that "which" isn't a part of POSIX, even.
- liquidise 5y agoMy story would echo this same sentiment. Incidentally, running `man command` on OS X actually does not reference a '-v' option at all, instead only stating the more verbose `command which`. Both appear to work, but it further highlights your (our) discoverability issue(s).
- createunderrate 5y ago`command which` just executes `which`
- Izkata 5y agoThere is no man or info page for "command" on Ubuntu, I guess it's just a bash builtin ("which command" doesn't find it). "which" has a man page though.
- rascul 5y agoFor bash, it is documented with 'help command' or the bash man page, section SHELL BUILTIN COMMANDS.
- sevensor 5y agoIn bash, such things are documented in the help system: $ help command command: command [-pVv] command [arg ...] Execute a simple command or display information about commands. Runs COMMAND with ARGS suppressing shell function lookup, or display information about the specified COMMANDs. Can be used to invoke commands on disk when a function with the same name exists. Options: -p use a default value for PATH that is guaranteed to find all of the standard utilities -v print a description of COMMAND similar to the `type' builtin -V print a more verbose description of each COMMAND Exit Status: Returns exit status of COMMAND, or failure if COMMAND is not found.
- denton-scratch 5y agoI didn't know about the "help" command in bash. I've always gone to "man bash", and then scrolled up and down for an hour or so. Or just cranked-up a search engine. So thanks for that.
- coolgeek 5y agoApparently, there's an "info" command, as well
- MonkeyClub 5y agoThough info isn't POSIX compliant. Not to mention that most distributions completely ignore it and don't populate it with a program's info documentation -- even when such exists.
- greyfade 5y agoNeither is Bash. And, being GNU projects, Bash and `info` generally come hand-in-hand.
- fractalb 5y agoWow. I never knew this. I tried scouring through the bash manpage to get hold of its inbuilt commands. Thanks.
- AceJohnny2 5y agoThe main problem is there is no good way to provide manpages for shell builtins, like `command`. Man can only have one entry (per section) for a given command, but what if you're using Bash? Or Zsh? How would it know which one to give you? If they're named differently (like a manpage for `bash-command` vs `zsh-command`), how would you know to look those up? (never mind that apropos would give you so-overloaded-to-be-useless results for something as generic as `command`) Which leaves us in the current situation where `man command` redirects to a generic `builtin` manpage, that doesn't have much info, and certainly nothing specific to Bash or Zsh.
- rascul 5y agoThere could potentially be a bash-<builtin> man page. For example, bash-command or bash-mapfile.
- creativemonkeys 5y agoOnce I tried to use `which` in cross-platform scripts, all sorts of issues crept up. The output and return result differ significantly between BSD, GNU and OSX, so you can't just run $(which python) and get a straightforward response back. Granted, I didn't know about `command -v` so I'm not sure if it works better in practice.
- kleiba 5y ago`which` and `command -v` give different results when you have aliased a command, e.g., `alias ls='ls --color=auto'`.
- Seirdy 5y agoYou should be using ShellCheck or an equivalent linter on your shell scripts in POSIX mode if you want them to be portable; in POSIX mode it warns when using "which" instead of "command -v", and I think it also warns when using "whence" or "where" outside of zsh. There's a host of other Bashisms/GNUisms and other noncompliant antipatterns it checks for too. It's also worth reading through the POSIX manpages for POSIX sh if you want to be a good shell scripter.
- quotemstr 5y ago> It's also worth reading through the POSIX manpages for POSIX sh if you want to be a good shell scripter. Or you can give the middle finger to POSIX, write shell scripts for bash, or even zsh, and end up with faster, more correct, and more beautiful shell scripts that in practice are just as portable as the ones that mindlessly follow the limited and bizarre POSIX shell spec. I'll write POSIXly-correct shell scripts when the POSIX people stop pretending it's still 1995.
- Woodi 5y ago> Or you can give the middle finger to POSIX Ok, pleas someone tell me how to use that "upvote" festure on HN :) More on the subject, nor type nor command, nor constantly mentioned shellcheck recipe for all posixs actually do what which do - just returns path ! Looks like with some options, maybe, but it could be simply added with "which -h" - (standard *nix human readable form toggle), if -h is not already taken. POSIX in the first place should just standarise which instead of inventing some "command". Advice: to write best POSIX scripts write moustly-POSIX scripts and be done. Simply becose you should write big shell scripts in the first place. It is Perl job and it do it very better. And if you want more you need C and actually using system calls yourself, eg. for precise path not-globbing. PS. Now just wait for Mr Poettering take on the subject becouse why be bothered by decades old working admins opinions or work conditions ? Or usable standards, or already working code, or not containers use cases, or just short, human friendly commandline ? Or not tangled and small codebases ?? Let's leave it to, I don't know, IBM ? Is systemd portable to AIX yet, any plans ? ;) WINDOWIZACION95 FTW
- 5y ago
- quotemstr 5y agoStandards are supposed to reflect time-tested practice, not the other way around. Instead of "fixing" every shell script on Earth to use "command -v" instead of the superior and perfectly functional which(1) command, we should just change POSIX and standardize which(1).
- tasuki 5y agoI rarely write/read shell scripts, and have been using "command -v" for years.