Re: which: command not found
Stephane Chazelas <[email protected]> Sat, 13 Dec 2025 08:27:21 +0000
| Newsgroups | gmane.comp.shells.zsh.devel,gmane.comp.shells.zsh.user |
|---|---|
| Message-ID | <nkhbx4xc7ivipyfugymga2sieja3wbmpva6bps4h55b7recez4@ddvj4qfodvfe> |
2025-12-12 17:12:55 -0500, Mark J. Reed: [...] > So the answer is "because csh did it that way and that's where zsh got the > 'which' command". Well, I guess that's why zsh also borrowed `whence` from > ksh, which behaves more sensibly here. which is the csh command (initially a csh script, see https://github.com/dspinellis/unix-history-repo/blob/BSD-3/usr/ucb/which) The Bourne shell equivalent is "type" (from SysVR2), the Korn shell equivalent "whence" (you'll see than in zsh, "which" is an alias for "whence -c", -c for csh), the POSIX equivalent "command -V" (POSIX also specifies "type", but optional (XSI)) Note that command -V in mksh, zsh and bosh at least send the "not found" to stdout. It's not cristal clear from the POSIX specification (https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/command.html) whether that's right or not. > I guess a consistent cross-shell approach here would be to use `type`, > check the return code, and extract the pathname from the end of the message > on success. [...] For the record, Bourne's "type" didn't return a non-zero exit status upon "command not found", and sent that "command not found" to stdout. The FAQ entry is a bit misleading at the moment in that the only other shell that I know that has "which" builtin is tcsh. Invoking which in other shells would run whatever which command is found in $PATH, and whatever that does depends on that which implementation. If it's the original which csh script (as it would still be on some commercial Unices), the "not found" will be sent on stdout. The which here on Ubuntu 24.04 doesn't report any error upon not found. See https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then for more of the gory details. -- Stephane