[PATCH] ash: add process substitution in bash-compatibility mode

Ron Yorston rmy at pobox.com
Wed Jan 30 11:10:16 UTC 2019


Denys wrote:
>On Mon, Jan 28, 2019 at 10:16 AM Ron Yorston <rmy at pobox.com> wrote:
>> Denys wrote:
>> >Can we use popredir() mechanism instead?
>>
>> It's possible but it adds more complication to already complex code.
>> Currently the stack of redir fds is always unwound unconditionally.
>> The procsub fds are conditionally removed depending on whether or not
>> we're in a shell function.
>
>That what looked unusual to me at once. Redirections should not depend
>on things like that.

Redirections shouldn't, but process substitutions aren't the same
as redirections.  My patch copies what bash does:  bash maintains a
separate record of process substitution file descriptors and treats
then differently depending on whether or not we're in a shell function.

Why does it do this?  The Bash Hackers Wiki [1] says this about the
scope of process substitutions:

   If a process substitution is expanded as an argument to a function,
   expanded to an environment variable during calling of a function, or
   expanded to any assignment within a function, the process substitution
   will be "held open" for use by any command within the function or
   its callees, until the function in which it was set returns.

The first two cases aren't problematic and are similar to redirections.
The third case is different.  Outside a function this doesn't work:

   $ SUB=<(echo hi)
   $ cat $SUB
   cat: /dev/fd/63: No such file or directory

Inside a function it does:

   $ f() {
   > SUB=<(echo hi)
   > cat $SUB
   > }
   $ f
   hi

Except...

The examples above were run on bash 4.1.2 in CentOS 6.  The same results
are obtained with bash up to version 4.2.45 (from 2013).  In bash 4.3
and later the third case described in the Wiki no longer works and the
second example also returns:

   cat: /dev/fd/63: No such file or directory

Other shells that support process substitution do the same.

It appears that the special treatment of process substitutions in
shell functions is both obsolete and a bashism.

I'll reconsider my patch in light of this.

Ron

[1] https://wiki-dev.bash-hackers.org/syntax/expansion/proc_subst


More information about the busybox mailing list