Live data from Hacker News

Which is not Posix

hynek.me

1–10 of 97 posts

Re: Which is not Posix

#2
Yeah, no thanks, I'm going to keep using `which`, and distributions are going to support it, no matter how "standard" `command -v` is. The only reason we're talking about this is that it's so noteworthy that any distro would seriously suggest people avoid `which`, which is the de facto standard.

Re: Which is not Posix

#4

macOS Catalina 10.15.7 $ command -v command command vs $ which command /usr/bin/command

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

#5
post #2

Yeah, no thanks, I'm going to keep using `which`, and distributions are going to support it, no matter how "standard" `command -v` is. The only reason we're talking about this is that it's so noteworthy that any distro would seriously suggest people avoid `which`, which is the de facto standard.

Busybox also supports both variants, which is nice.

    C:\Users\luser>busybox64 sh
    ~ $ command -v ssh
    C:/WINDOWS/System32/OpenSSH/ssh.exe
    ~ $ which ssh
    C:/WINDOWS/System32/OpenSSH/ssh.exe
    ~ $ exit

Re: Which is not Posix

#6
I do not understand this Debian 'which' kerfuffle, but seems a big argument over nothing.

But for me, 'type' is my go to in scripts. And as a tcsh user, I never knew 'which' was a thing in Bourne shells until I checked a few minutes ago. I do not know when it appeared, but I thought in the 'old days' 'which' was specific just to [t]csh.

Also, I never heard of 'command -v' until I saw it in LWN a few days ago :)

Re: Which is not Posix

#7
post #4

macOS Catalina 10.15.7 $ command -v command command vs $ which command /usr/bin/command

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`

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

#8
Not sure why the outcome of "`which` is not POSIX" would be "essentially everybody should stop using `which` and start using `command -v`, which is poorly known, has different semantics, and is covered in relatively little introductory material" rather than "this is just another example of why POSIX has been nearly irrelevant for a long time now".

systemd is not POSIX, either, but that's not taken as some justification for removing it from Debian – this whole drama is just pointless pedantry over something precisely because it is so low stakes.

Re: Which is not Posix

#9
Based on the title I was thinking this was a quiz. I am imagining a game where it shows two shell snippets and you need to select the one that works correctly under a strict POSIX environment.

Re: Which is not Posix

#10
OK, I don't get `command`. When I run `command time -v sleep 10` it clearly runs /usr/bin/time on my system (the bash builtin doesn't have the -v option), but if I run `command -v time` it outputs `time`, corresponding to the builtin. How is this helpful? Also shouldn't `command` and `command -v` find the same things?

edit: Well apparently this is the standard behavior [1]. So lookup is not the same for `command ` vs `command -v `, and there doesn't seem to be a standard way to lookup the executable started by `command `. `which` seem to fill that gap.

[1] https://pubs.opengroup.org/onlinepubs/007904975/utilities/co...

Post reply on HN