bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

posix command search and execution


From: Mike Jonkmans
Subject: posix command search and execution
Date: Mon, 6 Nov 2023 16:48:44 +0100

Hi,

I have some remarks/questions on POSIX Command Search and Execution,
related to bash and some to POSIX itself.


Introduction

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01
describes what to do when a simple command name needs to be resolved.
A rationale is in:
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html#tag_23_02_09_02

POSIX Command Search

According to these docs (what I make of it), resolving is done in steps,
the first applicable step is used:

1) No slash in the name.

1a) Run the builtin if the name matches that of a Special Builtin.

1b) List several names that have unspecified results.

1c) Use a function, for functions not matching standard utilities.

1d) Lists 20 fixed utility names (like alias, cd etc.) that are
    to be invoked at his point. No PATH search yet.
        These are the `regular builtins'.
        (These need not exist as builtin).

1e) Search via PATH.
1eI) Search is successful.
1eIa) Check for `regular builtins' and functions
      and invoke that regular builtin/function.
      Q: Shouldn't this specify an ordering for builtins/functions?
          Q: Why check for regular builtins? That was already done in 1d.
1eIb) Run the utility.
      (This is where ordinary builtins should run).
          (It seems logical that a builtin takes precedence over PATH).
1eII) Nothing in PATH, exit with 127

2) Slash in name? Try to execute; prescribed exits: 127 or 126.

I hope I have understood POSIX correctly on these points.


Bash Command Search

When not in posix mode, bash does:
- Ignore 1a, 1b, 1d.
- 1c) Just use the function. (Especially masking standard utilities).
- 1e) With 1eIa & b use builtins even if utility is not found in PATH.
Which is has a quite logical order: function, builtin, PATH.

In posix mode, it seems that bash:
- 1a) Honors special builtins (but see 1c).
- 1b) With source as a special builtin - which is ok (as unspecified).
- 1c) Doesn't allow you to define a function with the name
      of a special builtin.
      A function defined before `set -o posix' will mask a
          special builtin. (This seems to be ok).
      Utilities are masked by a function of that name.
- 1d) All names in the list, except newgrp, are bash-builtins
      and are used.
- 1eIa)
      Functions don't need a successful path search - per 1c.
- 1eIa & b)
      Builtins are also ran regardless of path search.
- *) ok.

Regarding source and 1c in bash, a mail from our beloved maintainer:
https://lists.gnu.org/archive/html/bug-bash/2014-03/msg00084.html

Thus in posix mode, bash does not follow this part of the standard.
But should it?
I would rather have POSIX modified to *also* accept the, more logical,
bash way (i.c. first matching functions, then builtins, then PATH).
Would that be a feasible modification to suggest to the Austingroup?


Remarks

- Regarding 1eIb.
  The shells posh, dash, ksh and zsh
  also run builtins, even when not found in PATH.
  Checked with the `test' builtin (mv /usr/bin/test{,.sav})
  on the versions found on Ubuntu 22.04.

- The 'newgrp' utility (mentioned in 1d) is not a builtin in bash.
  This is ok. The regular builtins from 1d need not be provided. See:
  https://lists.gnu.org/archive/html/bug-bash/2005-02/msg00129.html
  Builtins are defined in:
  
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_83
  Q: Isn't that incorrect in stating where regular builtins are defined?

- In bash's code builtins/builtins.c (made from builtins/mkbuiltins.c),
  the regular builtins are flagged with POSIX_BUILTIN.
  But `hash' is not. Omission or intention?
  Also, I don't see any use of the POSIX_BUILTIN flag. Remove?


POSIX Definitions

- A utility is mostly a builtin or executable:
  
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_22

- Utilities:
  https://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html
  Q: Where is `standard utilities' defined - as used in 1d.

- The POSIX definition of `regular builtin' is the same as those in 1d.
  
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tag_17_06
 
-- 
Regards, Mike Jonkmans



reply via email to

[Prev in Thread] Current Thread [Next in Thread]