bug-bash
[Top][All Lists]
Advanced

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

Re: Unexpected behaviour when using process substitution with stdout and


From: Greg Wooledge
Subject: Re: Unexpected behaviour when using process substitution with stdout and stderr
Date: Sun, 11 Jul 2021 09:17:40 -0400

On Sun, Jul 11, 2021 at 11:09:10AM +0100, earnestly wrote:
> What appears to be happening is that the output from standard error is
> being mixed into the function handling standard out, even more
> surprisingly that xtrace output is also being consumed and filtered as
> well.

First, xtrace (set -x) output is written to stderr by default.  You can
change this by setting the BASH_XTRACEFD variable.  But in the absence
of that change, it goes to stderr, along with any other content that's
written to stderr, either by bash itself, or by commands that bash calls.

> stdout() {
>     local line
> 
>     while read -r line; do
>         printf 'from stdout: %s\n' "$line"
>     done
> }
> 
> stderr() {
>     local line
> 
>     while read -r line; do
>         printf 'from stderr: %s\n' "$line"
>     done
> }

>     generate > >(stdout) 2> >(stderr)
>     wait

You're sending the stderr of generate to a subshell where the stderr()
function is executed.  But the stderr() function writes to stdout.
Therefore, all of the output from generate is ultimately ending up going
to the script's stdout, with potentially weird interleaving depending
on the timing of the two background subshells.

If you want to keep them separate, you either need to change the stderr()
function to write to stderr, or change the >(stderr) call to >(stderr >&2).



reply via email to

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