5

For some reason, it seems like stderr is being sent to stdout in the following bash script:

exec > >( while read line; do echo " stdout: $line"; done )
exec 2> >( while read line; do echo " stderr: $line"; done )

echo "rolo"
echo "cholo" >&2

if you run that, the output is so:

stdout: rolo
stdout: stderr: cholo

Does anyone know why that's happening? As far as I can tell, what's happening is that the stderr is being sent to stdout, that's why the first line is capturing the output from the second line?

1 Answers1

5

Yes — your standard error is being sent to standard output. It's what you asked to have happen.

Your exec 2> >(…) script is echoing to stdout, so the exec > >(…) script gets to see it too — that's why you have both stdout and stderr tags on the cholo output line.

Revise your script (bash17.sh) to:

exec  > >( while read line; do echo " stdout: $line"; done     )
exec 2> >( while read line; do echo " stderr: $line"; done >&2 )

echo "rolo"
echo "cholo" >&2

so that standard error goes to standard error, and you see:

$ bash bash17.sh
 stdout: rolo
 stderr: cholo
$ bash bash17.sh > /dev/null
 stderr: cholo
$ bash bash17.sh 2> /dev/null
 stdout: rolo
$

Clearly, to get the standard error to a file, you would use an alternative redirection inside the exec 2> >(…) component of the script.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I heard also that if you just put the `exec 2> >` above the first line, it would work also, but unconfirmed as of yet –  May 08 '18 at 06:46
  • I guess I don't understand why the `exec 2> >(…)` echoes to stdout? Why is taht? –  May 08 '18 at 06:49
  • Reversing the order of the `exec` operations should work. The `2>` process would have its standard output going to the original location; the `>` process would then only intercept other standard output. Without the `>&2` associated with the `2>`, the output would go to standard output and not standard error, of course, but it wouldn't be filtered/intercepted/processed by the other sub-process. – Jonathan Leffler May 08 '18 at 06:51
  • The sub-process in the `2>` notation has echo commands that write to their standard output. Since you've not changed the sub-processes output, it goes to the same place as the main shell's standard output. Where else would those echos echo to? Their input comes from the original standard error, but that's not a factor with where their output goes. – Jonathan Leffler May 08 '18 at 06:52
  • oh duh, yeah the echo will go to stdout, that now makes sense –  May 08 '18 at 07:19
  • any difference between this: –  May 08 '18 at 07:20
  • and the following instead: –  May 08 '18 at 07:20
  • 1
    Placing the `>&2` outside the loop means the redirection is done once, rather than once per line. In the sample script, there's only one line to read anyway, so it makes no odds. In the real world, it might make a marginal difference, but you'd be fairly hard put to measure it. – Jonathan Leffler May 08 '18 at 07:22
  • ah yes got it, makes sense –  May 08 '18 at 07:26
  • Note that if the redirection was `> file`, there'd be a major difference. Inside the loop, you'd clobber the file on each cycle, whereas outside, the lines would be written cumulatively. – Jonathan Leffler May 08 '18 at 07:28