Timeline for Why not use "which"? What to use then?
Current License: CC BY-SA 3.0
28 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 21, 2024 at 19:19 | comment | added | ron |
Asked 11 years, 3 months ago we should all meet up at a Waffle House to discuss this, as which is still around in RHEL-8 (and 9?) via which-2.21-20.el8.x86_64.rpm (from previously being contained in util-linux from the Linux Kernel Org). This comment brought to you by the kill -9 camp.
|
|
| Apr 27, 2024 at 21:00 | answer | added | mariotti | timeline score: 3 | |
| Aug 17, 2022 at 4:48 | answer | added | Martin Kealey | timeline score: 7 | |
| Jul 10, 2021 at 9:26 | answer | added | aryndin | timeline score: 8 | |
| Sep 10, 2019 at 8:53 | comment | added | MikeRoger | Highly ranked Q&A at stackoverflow.com/questions/592620/… | |
| Feb 5, 2019 at 16:46 | comment | added | Stéphane Chazelas |
@mirabilos, most which implementations including the original csh script (as found on most commercial/traditional Unices) or the tcsh builtin don't have a -a option. In zsh, the equivalent of which -a would be whence -ca, not whence -pa which only reports the paths of executables.
|
|
| Feb 5, 2019 at 16:35 | comment | added | mirabilos |
I just use whence -p on mksh, but there’s no equivalent for which -a, so alias which='whence -p' will fail (or force you to use \which -a)… but the POSIX sh command command is probably your best bet.
|
|
| May 3, 2017 at 15:19 | comment | added | Stéphane Chazelas |
@rudimeier, again, there are a number of conditions under which many which implementations would not give you even the ls that would be found by a look-up of $PATH (regardless of what ls may invoke in your shell). sh -c 'command -v ls', or zsh -c 'rpm -q --whatprovides =ls' are more likely to give you the correct answer. The point here is that which is a broken heritage from csh.
|
|
| May 3, 2017 at 14:03 | comment | added | rudimeier |
@StéphaneChazelas Don't you get my point? I am NOT interested about builtin, function or aliases. I WANT the path to the executable file name. The fact that ls is a function on my system is the best reason why to use which in this case. Instead of stat my real usage is often this one rpm -q --whatprovides $(which ls). And yes, I don't use quotes interactively when I know that I don't need any. My PATH never contains whitespaces otherwise I consider it a bug in my setup and accept undefined behavior.
|
|
| May 3, 2017 at 13:38 | comment | added | Stéphane Chazelas |
@rudimeier, (stat $(which ls) is wrong for several reasons (missing --, missing quotes), not only the usage of which). You'd use stat -- "$(command -v ls)". That assumes ls is indeed a command found on the file system (not a builtin of your shell, or function of alias). which might give you the wrong path (not the path that your shell would execute if you entered ls) or give you an alias as defined in the configuration of some other shells...
|
|
| May 3, 2017 at 13:32 | comment | added | rudimeier |
@StéphaneChazelas How would I rewrite stat $(which ls) using type or command -v? I'm usually just interested in "looking for a path to an executable". which seems to be exactly the right tool for this. I see what your question and answer is pointing to, but this is not about all use cases of which.
|
|
| May 3, 2017 at 13:05 | comment | added | Stéphane Chazelas |
@rudimeier, then the answer would be always unless your shell is (t)csh (or you don't mind if it doesn't give you the correct result), use type or command -v instead. See the answers for why.
|
|
| S Nov 5, 2016 at 0:00 | history | suggested | mbigras | CC BY-SA 3.0 |
minor grammer
|
| Nov 4, 2016 at 23:49 | review | Suggested edits | |||
| S Nov 5, 2016 at 0:00 | |||||
| Sep 3, 2016 at 1:43 | history | edited | Jeff Schaller♦ | CC BY-SA 3.0 |
added 2 characters in body
|
| Sep 24, 2014 at 11:29 | history | rollback | Stéphane Chazelas |
Rollback to Revision 5
|
|
| Sep 6, 2014 at 22:47 | history | edited | Faheem Mitha | CC BY-SA 3.0 |
edited title
|
| May 21, 2014 at 6:54 | comment | added | Stéphane Chazelas |
@MattBianco, yes, csh (and which is still a csh script on most commercial Unices) does read ~/.cshrc when non-interactive. That's why you'll notice csh scripts usually start with #! /bin/csh -f. which does not because it aims to give you the aliases, because it's meant as a tool for (interactive) users of csh. POSIX shells users have command -v.
|
|
| May 21, 2014 at 6:37 | comment | added | MattBianco |
I think most arguments against using which are assuming an interactive shell context. This question is tagged /portability. So i interpret the question in this context as "what to use instead of which to find the first executable of a given name in the $PATH". Most answers and reasons against which deal with aliases, builtins and functions, which in most real-world portable shell scripts are just of academic interest. Locally defined aliases aren't inherited when running a shell script (unless you source it with .).
|
|
| Aug 8, 2013 at 20:58 | answer | added | Stéphane Chazelas | timeline score: 69 | |
| Aug 8, 2013 at 19:45 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 |
edited title
|
| Aug 6, 2013 at 9:49 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 |
deleted 3 characters in body
|
| Aug 2, 2013 at 9:32 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 |
added 27 characters in body; edited tags
|
| Aug 2, 2013 at 7:53 | answer | added | Chris Down | timeline score: 27 | |
| Aug 2, 2013 at 3:13 | history | tweeted | twitter.com/#!/StackUnix/status/363135454280810496 | ||
| Aug 1, 2013 at 23:21 | history | edited | Stéphane Chazelas |
edited tags
|
|
| S Aug 1, 2013 at 22:58 | answer | added | Stéphane Chazelas | timeline score: 563 | |
| S Aug 1, 2013 at 22:58 | history | asked | Stéphane Chazelas | CC BY-SA 3.0 |