On Tue, 17 Jan 2023 at 03:36, <dave.dram...@gmail.com> wrote:

> The following script attempts to read the first column of data, line by
line, into a variable called `$var`. With `BASH_XTRACEFD` unset, it
propertly prints r1c1, r2c1, r3c1, r4c1. With `BASH_XTRACEFD=1`, `$var`
gets set to +, ++, r2c1, r3c1, r4c1. Maybe I am just being dumb?

> echo -e $data | while read -r var junk; do
>    echo "var = $var"
> done

Technically the behaviour you're observing is correct: whenever set -x is
in effect, each shell process prints the command it's about to execute
after variable substitutions and globbing have been done, and those happen
after redirections have been done.

So the echo that's on the LHS of a pipe is run by a Bash process that
already has stdout connected to the pipe, before it prints "+ echo..." to
stdout.

You can get the effect you want by changing
    BASH_XTRACEFD=1
to
    exec {BASH_XTRACEFD}>&1

-Martin

Reply via email to