On 10/16/25 5:02 PM, Martin D Kealey wrote:
In Posix mode, Bash exports functions as well as variables when `set -o allexport` (`set -a`) is in effect.
Yes. POSIX doesn't say anything about exporting functions, so this is a bash extension. There isn't a conformance issue here.
This behaviour does not appear to match Posix' description of `set` in issue 8 (§2.15) <https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_26> (*1), which mentions *variable assignments* (and notes that certain implicit or indirect assignments are included) but mentions neither *functions* nor any term that could be construed as including them.
True, POSIX does not contemplate exporting functions. It's outside the scope of the standard.
Brief tests of ksh & zsh (*2) indicate that those shells do not exhibit this behaviour; rather, ‘set -a’ causes them to export only variables, regardless of emulation/compatibility settings, in line with the Posix description.
Correct, neither zsh or ksh allow you to export functions.
Bash's man & info pages describe this behaviour thus:
‘-a’
Each variable or function that is created or modified is given
the export attribute and marked for export to the environment
of subsequent commands.
…so I guess this could be construed as a Bash extension to the standard.
Yes.
However `help set` does *not* mention functions: -a Mark variables which are modified or created for export.
OK.
Anyway, it seems to me that exporting functions when in Posix mode could be construed as a bug.
No. Bash correctly implements the standard and will run POSIX-conforming scripts without problems.
Maybe exporting functions as a result of `allexport` should be disabled when in Posix mode, and documented as an extension otherwise? Does that seem reasonable?
Why?
Relatedly, does it make sense for a variable to have both the ‘nameref’ and ‘exported‘ attributes?
It's kind of weird, but since you can't export attributes, it's harmless.
Chet, do you remember why this extension was implemented? Was it a case of mimicking some other shell, or based on something else? A quick hunt through the git history shows that `bind_function` has included a check for `mark_modified_vars` since before the git history started at Bash-v1.14.7.
It came in when we started allowing exporting functions. If you don't want
to inherit possibly-exported functions, you can start bash with the `-p'
option, which does that and a few other things.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU [email protected] http://tiswww.cwru.edu/~chet/
OpenPGP_signature.asc
Description: OpenPGP digital signature
