help-bash
[Top][All Lists]
Advanced

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

Re: why are pipeline commands (allowed to be) executed in subshells?


From: Davide Brini
Subject: Re: why are pipeline commands (allowed to be) executed in subshells?
Date: Fri, 25 Nov 2022 19:22:42 +0100

On Fri, 25 Nov 2022 18:51:47 +0100, Philippe Cerfon <philcerf@gmail.com>
wrote:

> meaning that there is one subshell per command of the pipeline, and
> not one for all.
> 
> It's clear that any non-built-in command/programs are executed not in
> the same shell environment, but why are all shell commands?
> 
> Any what in POSIX even allows this?
> 
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html:

Section 2.12, shell execution environment:

...Command substitution, commands that are grouped with parentheses, and
asynchronous lists shall be executed in a subshell environment.
** Additionally, 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. All other commands shall be
executed in the current shell environment.

Bash has "shopt -s lastpipe" to optionally execute the last component of a
pipeline in the current environment.

In general, safe programs should not rely/depend on any element of a
pipeline being executed in a subshell or in the current environment.

-- 
D.



reply via email to

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