4 ms·
> Use command -v instead Huh? That's a shell builtin, not an executable. That's next to useless. > which is widespread but not standardized What can possibly
by jfrunyon 5y ago
> Use command -v instead
Huh? That's a shell builtin, not an executable. That's next to useless.
> which is widespread but not standardized
What can possibly need standardization about "prints the full path to a command"???
> which in its barest form fails to handle builtins and aliases
Yes, because it's an executable, not a builtin. This means that it is usable from outside of a shell, which is quite a significant benefit. Additionally, there is no path to a builtin or alias - so it actually handles both how it says on the tin: it returns no path. Moreover, it's rarely useful to check for a builtin or alias from a non-interactive context.
> Just compare the arguments of the most common ones
I have literally never once had to pass a single argument to which, except the name of a command. At which point it has literally always given me back the path to that command (or nothing).
But, let's compare, shall we?
- All 4 implementations support `which which`, with identical semantics
- All 4 implementations support `which -a which`, with identical semantics
- 2 implementations support no other options
- 1 implementation has a -s flag for silent operation, which can be portably replaced by redirecting output.
- 1 implementation has many more options... which appear to mostly be useful interactively. I'm not sure why this implementation is presented as "GNU"; the linked page clearly attributes it to a "Carlo Wood"
> For scripts, command -v is better, because it’s simpler and machine-readable.
I strongly doubt that `command -v` actually does what the author wants it to do. I can't imagine why you would want to end up with the string "alias ls='ls --color=auto'" in your variable when you were looking for "/bin/ls", as `which` would return.
- hynek 5y agoI'm super confused by the hostility of this comment. The TIL post was just a summary of a drama that almost removed which from debian and the lessons learned from it about the guarantees and portability of which (none), and how to prevent problems in the future. You can freely choose that the problems don't apply to you, and you can be angry at POSIX and/or Debian for whatever happened or not as many others in this comment section. But I don't understand why you chose to write a belligerent comment as if any of it is the author's (yes me, but I didn't submit it to HN) fault or as if he is arguing in front of a committee to take it away from you. As for Carlo Wood: https://savannah.gnu.org/projects/which https://savannah.gnu.org/projects/which links to http://www.gnu.org/software/which/ http://www.gnu.org/software/which/ that in turn links to https://carlowood.github.io/which/ https://carlowood.github.io/which/. Maybe do your research if you want to be a pedant?