Timeline for Why not use "which"? What to use then?
Current License: CC BY-SA 4.0
53 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 21, 2024 at 7:29 | comment | added | Stéphane Chazelas | @einpoklum, thanks. I've changed the ls, echo variable names to ls_path, echo_path. | |
| Nov 21, 2024 at 7:29 | history | edited | Stéphane Chazelas | CC BY-SA 4.0 | added 53 characters in body |
| Nov 21, 2024 at 7:19 | comment | added | einpoklum | suggest the example not assign the path to ls, to a variable named ls. THat is rather confusing. | |
| Mar 2, 2024 at 23:16 | comment | added | Seamus | This is a truly magnificent "chronicle" on the which command. Now - if we only had one for whereis, I'd be most happy :) | |
| S Jul 12, 2022 at 7:02 | history | suggested | Akbarkhon Variskhanov | CC BY-SA 4.0 | Duplicating stderr from stdout is unnecessary since `command [-v|V]` write to stdout, as mandated by POSIX. |
| Jul 12, 2022 at 6:59 | review | Suggested edits | |||
| S Jul 12, 2022 at 7:02 | |||||
| Apr 26, 2022 at 1:20 | comment | added | Rick | @user2357112 Agree. I downvoted. So many history and useless info. | |
| Dec 1, 2021 at 17:28 | comment | added | anton_rh | command -v ls prints alias ls='ls --color=auto'. But I need the path to ls executable. So which is still better for getting path to executable. | |
| Apr 11, 2021 at 15:39 | history | edited | Stéphane Chazelas | CC BY-SA 4.0 | added 78 characters in body |
| May 17, 2020 at 15:44 | history | edited | Jonathan Leffler | CC BY-SA 4.0 | Fix trivial typos |
| Sep 3, 2019 at 21:02 | comment | added | user232326 | No command -v which doesn't provide the *full path to which` in zsh for example. That is not a portable solution. No command -v given-command doesn't tell if an external executable exists, it also include builtins. Try zsh -c 'command -v exit; echo $?' There is no external command exit, it makes no sense. As an executable for cd also makes no sense. But the shell answer with success (0). | |
| Jun 17, 2019 at 5:07 | comment | added | Manngo | This is a very thorough exposé. However, what is meant by using ls=$(command -v ls)? Doesn’t it assign the output to a variable $ls`? | |
| Feb 5, 2019 at 20:15 | history | edited | Stéphane Chazelas | CC BY-SA 4.0 | added 4 characters in body |
| Sep 25, 2018 at 6:48 | history | edited | Stéphane Chazelas | CC BY-SA 4.0 | added 4 characters in body |
| Sep 24, 2018 at 22:10 | history | edited | Stéphane Chazelas | CC BY-SA 4.0 | deleted 105 characters in body |
| Mar 30, 2018 at 20:29 | history | edited | user137369 | CC BY-SA 3.0 | Shorter way to redirect to /dev/null |
| Nov 24, 2017 at 9:08 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | edited body |
| Sep 5, 2017 at 8:14 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | edited body |
| Jul 3, 2017 at 6:10 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 167 characters in body |
| Jul 3, 2017 at 5:59 | comment | added | Stéphane Chazelas | @jarno, oh yes, you're right. bash will settle on a non-executable file if it can't find an executable one, so it's "OK" (though in practice one would rather command -v/type return an error) as that's the command it would try to execute when you run mytestfile, but the dash behaviour is buggy, as if there's a non-executable cmd ahead of an executable one, command -v returns the non-executable one while executing cmd would execute the executable one (the wrong one is also hashed). FreeBSD sh (also based on ash) has the same bug. zsh, yash, ksh, mksh, bash as sh are OK. | |
| Jul 2, 2017 at 20:30 | comment | added | jarno | @StéphaneChazelas If I create new file by touch /usr/bin/mytestfile and thereafter run command -v mytestfile, it will give the path (whereas which mytestfiledoes not). | |
| Jul 2, 2017 at 16:52 | comment | added | Stéphane Chazelas | @jarno, I can't reproduce. Possibly you've tested by removing the execute permission to an executable that had been hashed while it had execute permission (in which case the shell would try to execute it by that path so command -v would be right to report that path). | |
| Jul 2, 2017 at 8:43 | comment | added | jarno | In contrary to what the answer claims, command -v does not check for execute permission, at least if you do call it by pure file name argument without path. I tested by dash 0.5.8 and GNU bash 4.3.48. | |
| Sep 3, 2016 at 2:06 | comment | added | Kaz | command is described by POSIX. | |
| Jun 10, 2016 at 1:42 | history | edited | agc | CC BY-SA 3.0 | Typo "can just to" |
| Oct 19, 2015 at 13:04 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 13 characters in body |
| Feb 22, 2015 at 11:27 | comment | added | Milliways | This rant may have some basis (debian has a which /bin/sh script). OS X (which is BSD) has an exe and the man states "The which utility was originally written in Perl and was contributed by Wolfram Schneider <[email protected]>. The current version of which was rewritten in C by Daniel Papasian <[email protected]>. June 21, 2002 BSD" | |
| Feb 9, 2015 at 20:19 | comment | added | mikeserv | command's return is spec'd like: 126 The utility specified by command_name was found but could not be invoked. 127 An error occurred in the command utility or the utility specified by command_name could not be found. Otherwise, the exit status of command shall be that of the simple command specified by the arguments to command.... and so, in a lot of cases, it could be more useful just to run command command straight off and test $? after rather than looking for it first with command -v. You can limit its path search w/ command -p as well. | |
| Nov 26, 2014 at 19:29 | history | edited | tshepang | CC BY-SA 3.0 | too meta |
| Aug 14, 2014 at 9:31 | comment | added | null | In fish shell, type -fp directly points to which. The only difference is that it shows an error message on <del>STDERR</del> STDOUT if the file doesn't exist. There are no advantages for using type -fp over which, and generally which is more readable. | |
| Jul 24, 2014 at 1:11 | history | edited | Gilles 'SO- stop being evil' | CC BY-SA 3.0 | historical: OSF1's `type` builtin only started returning 1 on a nonexistent command name in v4 |
| Mar 8, 2014 at 12:24 | comment | added | user2357112 | After reading this post, I have found a lot of context for the answer, but not the answer itself. Where in this post does it actually say why not to use which, as opposed to things you might be trying to use which to do, the history of which, implementations of which, other commands to do related tasks, or reasons to actually use which? Why are the other commands better? What do they do differently from which? How do they avoid its pitfalls? This answer actually spends more words on the problems with the alternatives than the problems with which. | |
| Jan 21, 2014 at 15:01 | comment | added | user | @jordanm TL;DR: type which? | |
| Jan 21, 2014 at 14:48 | history | edited | null | CC BY-SA 3.0 | Add type command from fish shell |
| Nov 30, 2013 at 18:44 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 4 characters in body |
| Aug 10, 2013 at 13:20 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 1419 characters in body |
| Aug 10, 2013 at 11:02 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 211 characters in body |
| Aug 9, 2013 at 9:33 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 1675 characters in body |
| Aug 8, 2013 at 21:07 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 21 characters in body |
| Aug 6, 2013 at 11:23 | comment | added | Rob | @Joe FreeBSD uses csh for system scripts. | |
| Aug 6, 2013 at 8:56 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 184 characters in body |
| Aug 6, 2013 at 7:06 | history | made wiki | Post Made Community Wiki by Stéphane Chazelas | ||
| S Aug 5, 2013 at 21:48 | history | edited | jasonwryan | CC BY-SA 3.0 | add section headings to make it easier to skim the answer; add a tl;dr section at the top for people who just want to know the answer |
| S Aug 5, 2013 at 21:48 | history | suggested | Richard Hansen | CC BY-SA 3.0 | add section headings to make it easier to skim the answer; add a tl;dr section at the top for people who just want to know the answer |
| Aug 5, 2013 at 21:29 | review | Suggested edits | |||
| Aug 5, 2013 at 21:48 | |||||
| Aug 5, 2013 at 7:19 | comment | added | Bananguin | I suppose I won't be using which anymore. Served me well for a very long time. Never ever used anything but bourne shells through ... | |
| Aug 3, 2013 at 7:39 | comment | added | Stéphane Chazelas | @Joe, which is a csh script on many commercial Unices. The reason is historical, that's why I gave the history, so people understand where it came from, why people got used to using it and why actually there's no reason you should be using it. And yes, some people use (t)csh. Not everybody uses Linux yet | |
| Aug 2, 2013 at 19:13 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 161 characters in body |
| Aug 2, 2013 at 7:11 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 623 characters in body |
| Aug 2, 2013 at 6:47 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 9 characters in body |
| Aug 1, 2013 at 23:19 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 258 characters in body |
| Aug 1, 2013 at 23:06 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 133 characters in body |
| Aug 1, 2013 at 22:58 | history | answered | Stéphane Chazelas | CC BY-SA 3.0 |