Skip to content
Snippets Groups Projects

`command -v` portability

Merged Eli Schwartz requested to merge eschwartz/xdg-utils:command-v-portability into master
5 unresolved threads

The "which" utility is not guaranteed to be installed, and if it is, its behavior is not portable.

Conversely, the "command -v" shell builtin is required to exist in all POSIX 2008 compliant shells, and is thus guaranteed to work everywhere.

Examples of open-source shells likely to be installed as /bin/sh on Linux, which implement the 12-year-old standard: ash, bash, busybox, dash, ksh, mksh and zsh.

A side benefit of using the POSIX portable option is that it does not require an external disk executable to be forked. This therefore represents a mild speedup.

Merge request reports

Merged by David FaureDavid Faure 2 years ago (Nov 6, 2022 2:01pm UTC)

Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Eli Schwartz mentioned in merge request !43 (closed)

    mentioned in merge request !43 (closed)

  • Eli Schwartz added 5 commits

    added 5 commits

    • d11b33ec - 1 commit from branch xdg:master
    • fb51a047 - kill off whitespace errors
    • 17c7c995 - remove bashisms from POSIX sh script
    • d7b10b42 - command_exec: run external command using env instead of which
    • 79026aab - Replace all remaining usage of non-portable 'which' with POSIX command -v

    Compare with previous version

  • Author Contributor

    Apparently so.

    But it's not entirely clear what to do or who to ask in order to get anything merged. Hence my MR has been outstanding for a year, and is collecting duplicates.

    Oh well, at least the code doesn't change often enough for gitlab's autorebase to be unable to figure out how to rebase. ;)

    (Then again that itself might be worrisome.)

    • It is okay. I just mentioned the relevant issue number for GitLab links.

      Your merge request suggests to remove redirection of stderr, 2>&1. Is it safe? POSIX does not forbid printing diagnostic messages of the command utility. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html

      Do we not end up with multiple useless output when trying to open something? Like

      command: qwerty: not found

      I have checked Bash and Dash, their command prints nothing on such an error.

    • Author Contributor

      On the contrary, my reading of POSIX is that it does forbid printing diagnostic messages... when using the -v option.

      Otherwise, no output shall be written and the exit status shall reflect that the name was not found.

      I'm not aware of a shell that does print diagnostics on the -v option.

      The other use of command to run a command, not print the location of the command, does print diagnostics.

    • Please register or sign in to reply
    • BusyBox built-in shell also prints nothing when a command is not found.

      BusyBox v1.30.1 (Ubuntu 1:1.30.1-6ubuntu3.1) built-in shell (ash)
      Enter 'help' for a list of built-in commands.
      
      ~ $ command -v qwerty
      ~ $ echo $?
      127
    • Author Contributor

      Nor do ksh, zsh, mksh...

      Busybox sh (actually ash) was created by importing the dash source code into the busybox git tree.

      Periodically, the Busybox project imports commits of the form "Upstream commit: ..." in order to re-sync dash changes, though busybox ash has expanded the scope of the original code import (for example, it has a CONFIG_ASH_BASH_COMPAT: "bash-compatible extensions").

      So they are likely to behave the same or similar. :)

    • Please register or sign in to reply
  • mentioned in issue #196 (closed)

  • Jarno Suni
    Jarno Suni @jarnos started a thread on commit d7b10b42
321 320 args=$(( $args - 1 ))
322 321 done
323 322 [ $replaced -eq 1 ] || set -- "$@" "$target"
324 "$command_exec" "$@"
323 env "$command" "$@"
  • An alternative would be to use command instead of env, but that would run a special built-in with exit code 0, if such a built-in name was passed as the first argument.

  • Author Contributor

    Right. The commit message implies the problem of command though the fact that it's not part of the other changes to use command -v, but it doesn't say so straight out I guess.

  • Please register or sign in to reply
  • Eli Schwartz mentioned in merge request !60 (closed)

    mentioned in merge request !60 (closed)

  • Author Contributor

    I'd be delighted to get this in, and am more than happy to accommodate any review feedback, of which there has been (to date) exactly zero of.

    Really not sure how to help this MR move along. :( But I live in eternal hope, one day...

  • Eli Schwartz mentioned in merge request !52 (merged)

    mentioned in merge request !52 (merged)

  • Eli Schwartz added 5 commits

    added 5 commits

    • 1a58bc28 - 1 commit from branch xdg:master
    • 286c020f - kill off whitespace errors
    • 3a942c88 - remove bashisms from POSIX sh script
    • abcf02fb - command_exec: run external command using env instead of which
    • 826699ad - Replace all remaining usage of non-portable 'which' with POSIX command -v

    Compare with previous version

  • Eli Schwartz added 4 commits

    added 4 commits

    • 2db83fdc - kill off whitespace errors
    • 7f873416 - remove bashisms from POSIX sh script
    • cd5c010b - command_exec: run external command using env instead of which
    • bd21104b - Replace all remaining usage of non-portable 'which' with POSIX command -v

    Compare with previous version

  • Eli Schwartz added 5 commits

    added 5 commits

    • 1a58bc28 - 1 commit from branch xdg:master
    • 3eaf2f3c - kill off whitespace errors
    • 1d580420 - remove bashisms from POSIX sh script
    • 80d025ae - command_exec: run external command using env instead of which
    • 8ae02631 - Replace all remaining usage of non-portable 'which' with POSIX command -v

    Compare with previous version

  • merged

  • Slatian mentioned in merge request !115 (merged)

    mentioned in merge request !115 (merged)

  • Slatian mentioned in commit f113a8b9

    mentioned in commit f113a8b9

  • Please register or sign in to reply
    Loading