Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH v2] exec: run final pipeline command in a subshell in sh mode

On 2020-06-05 at 10:21:41, Mikael Magnusson wrote:
> On 6/5/20, brian m. carlson <sandals@xxxxxxxxxxxxxxxxxxxx> wrote:
> > zsh typically runs the final command in a pipeline in the main shell
> > instead of a subshell.  However, POSIX requires that all commands in a
> > pipeline run in a subshell, but permits zsh's behavior as an extension.
> What POSIX actually says is:
> "each command of a multi-command pipeline is in a subshell
> environment; as an extension, however, any or all commands in a
> pipeline may be executed in the current environment"
> Ie, it does not say "shall", so it doesn't require a subshell all, in
> fact it explicitly does permit not using one as you also say. The
> patch is possibly useful (seems unlikely to me), but to say it is
> required by POSIX is not true. If someone depends on every command in
> a pipeline being a subshell, they should fix their code, for example
> by adding ( ) around it (the command(s) or the whole pipeline).

POSIX makes a declarative statement about the behavior of a pipeline.
It is true that it doesn't explicitly use the word "shall" in this case,
since such a statement would explicitly prohibit the inclusion of an
extension at all and make it explicitly non-conforming.

What POSIX does say is that one “shall define an environment in which an
application can be run with the behavior specified by POSIX.1-2017.”
I'm proposing that "zsh --emulate sh" implement the POSIX behavior for
that reason.

I will tell you that as a practical matter, nobody writing code for sh
expects the last command not to be run in a subshell and consequently
lots of code is practically broken in this case with zsh as /bin/sh.
The Git Project is very fastidious about writing portable shell, as is
Debian, and I can tell you from experience that both are broken with zsh
as sh with the current behavior, even if they should not have made that

zsh is a very popular interactive shell, and allowing it to be used as a
portable sh on systems where the system sh is less capable would be
really beneficial.  I would also like to see macOS users who decide to
use zsh as /bin/sh have a good experience with existing code that
overwhelmingly does make this assumption.

If your objection is to the wording, I'm happy to revise it to remove
the word "requires", but I do think this provides a lot of benefits for
the sh scripting case while not impacting users who are expecting
different behavior for the zsh case.
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

Attachment: signature.asc
Description: PGP signature

Messages sorted by: Reverse Date, Date, Thread, Author