News for hackers. Stuff that matters.
Re:Which is not Posix by mistersquid on Monday November 01, 2021 @12:34PM (#29069094)
macOS Catalina 10.15.7

  $ command -v command
  command
vs

  $ which command
  /usr/bin/command
Re:Which is not Posix by dahfizz on Monday November 01, 2021 @12:38PM (#29069175)
command is a bash builtin. /usr/bin/command is a bash script that calls into the builtin command.

If you just run `command`, you are running the builtin function and not the /usr/bin/command file.

Try running `command -V command` and `command -V /usr/bin/command`

Re:Which is not Posix by steerablesafe on Monday November 01, 2021 @12:47PM (#29069299)
Point being, it doesn't help you finding /usr/bin/command via regular search in PATH and it reports the builtin instead.
Re:Which is not Posix by joemi on Monday November 01, 2021 @02:55PM (#29071240)
Isn't reporting the builtin the best behavior, since that's what will run if you type `command`?
Re:Which is not Posix by pxc on Monday November 01, 2021 @04:37PM (#29072714)
No. There is no ‘best behavior’.

If your aim is to discover which piece of software provides an executable you know you're running, whether it's wrapped by an alias or not, you want the behavior of `which`, and the behavior of `command -v` won't help you.

If your aim is to discover what will run when you type a command in your shell, the behavior of `command -v` (or `type`) is what you want, and the `which` program won't help you.

Re:Which is not Posix by SAI_Peregrinus on Monday November 01, 2021 @10:44PM (#29076287)
Exactly. I've been troubleshooting updating some libraries on an embedded system recently, and running a lot of `/lib/ld-linux.so --list "$(which <executable>)"` on various things to figure out what `-rpath`s I need to pass to the linker. I want the command to fail if I give it a builtin or alias!

Other times I want to use `command`: most often to bypass an alias, but with `-v` mode it's handy to get the definition of an alias.

I suppose `find "$PATH" -name NAME` is roughly equivalent to `which NAME`.

Re:Which is not Posix by pxc on Tuesday November 02, 2021 @12:17AM (#29077015)
Is calling `ld-linux.so` as an executable like that the same thing as calling `ldd`? is there any reason to prefer to invoke it that way?

> I suppose `find "$PATH" -name NAME` is roughly equivalent to `which NAME`.

Only if you're on Fish! Remember that ‘normal’ shells use `:` as the PATH separator :)