Menu

#220 Avoid using non-POSIX `which`

none
closed
None
requested
6
2024-12-08
2024-12-08
Viorel
No

which is not POSIX. Some distributions are trying to deprecate and remove it.

Please see this discussion: https://lwn.net/Articles/874049/
Also the related bug in Gentoo Linux: https://bugs.gentoo.org/942752

The POSIX way would be to replace which with command -v, but in bash type -P works usually better (not POSIX either), while in busybox ash both command -v sh and type -P sh can return sh, without the path. So I think it's best to avoid it altogether.

There are 2 occurences:

  • for the one in the Makefile I replaced which sh with $(SHELL), this should always be set. It can also be hardcoded to /bin/sh if needed.
  • for the one in build_tree.sh I simply removed it and ignored the error if missing, that's what the if was doing.
1 Attachments

Discussion

  • Denis Corbin

    Denis Corbin - 2024-12-08
    • status: unread --> open
    • assigned_to: Denis Corbin
    • Priority: 5 --> 6
     
  • Denis Corbin

    Denis Corbin - 2024-12-08

    Thank you for this feedback, I will review and integrate your patch proposal and publish it very soon with release 2.7.16

     
  • Denis Corbin

    Denis Corbin - 2024-12-08

    I just noted that 'command -v' is not available under tcsh shell, where "which" is a shell built-in command.

    I find really weird to request a change of user habits (and existing scripts expectations) of using "which", just because there are different implementations, and ask it be be replaced it with something completely different "command -v" or other things that do not exist in every environment unlike "which"...

    if some "which" implementations provide additional features by mean of -a or a -s option, they all work the same without any option and this is that way for at least three decades. If some homogeneity is needed, a better way would be to standardize it by defining the common behavior (= "which" without options) while any option being known as non-standard, which is de-facto the current status AFAIK. Developer and user could still use non-standard extension or avoid using them depending on the context (as it is today when writing shell script with or without bash extensions... se the configure script produced by autoconf for example).

    If then new features are needed in a standard way (to do what -s or -a options actually do), another tool could be specified for that (command -v), or the standard could extend the "which" behavior... but at no point in time it is a good thing to break habits and usages when there are paths to avoid it, in my very humble opinion.

    Anyway, I've applied this Gentoo patch.

     
  • Viorel

    Viorel - 2024-12-08

    I agree with you, I don't like the status quo either. which is everywhere, while the "standard" way of doing things is not. That's why I tried to avoid it rather than replace it, and I did this only to be prepared in case mainstream distributions start dropping it.

    Thank you for applying the patch.

     
  • Denis Corbin

    Denis Corbin - 2024-12-08

    I usually add a note in the THANKS file and release notes for contributions and feedback as yours. I will thank "Viorel at Sourceforge" unless you prefer something else? Let me know :)

     
  • Viorel

    Viorel - 2024-12-08

    My full name is Viorel Munteanu. Thanks!

     
  • Denis Corbin

    Denis Corbin - 2024-12-08

    Git is up to date, I expect to release 2.7.16 tomorrow.
    Closing this report.
    Thanks again

     
  • Denis Corbin

    Denis Corbin - 2024-12-08
    • status: open --> closed
     

Log in to post a comment.

MongoDB Logo MongoDB