bug-bash
[Top][All Lists]
Advanced

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

Re: new redirection operator seems broken


From: Greg Wooledge
Subject: Re: new redirection operator seems broken
Date: Thu, 12 Mar 2009 12:36:49 -0400
User-agent: Mutt/1.4.2.2i

On Thu, Mar 12, 2009 at 05:21:04PM +0100, Andreas Schwab wrote:
> Still does not fix this case:
> 
> $ echo >&2 |& wc -l
> 
> 0

That looks like the correct output to me.  When setting up a pipeline
with redirections, the pipeline happens first.

The manual says your example should be equivalent to

  echo >&2 2>&1 | wc -l

In that case, the redirections are done in this order:

 1) The pipe is created.  The first process's stdout goes to the pipe.
 2) >&2 is done, sending the first process's output to script-stderr.
 3) 2>&1 is done, sending the first process's stderr to script-stderr.

End result: echo's output and stderr are both going to script-stderr,
not to the pipe.

To test what I suspect you were actually trying to test, you need the
redirection to occur in the child process, instead of in the pipeline.
Like this:

imadev:/var/tmp/bash-4.0$ echo foo >&2 |& tr '[:lower:]' '[:upper:]'
foo
imadev:/var/tmp/bash-4.0$ (echo foo >&2) |& tr '[:lower:]' '[:upper:]'
FOO

A very subtle but important distinction there.  Usually when I'm testing
complex redirection stuff, I'll make a function that writes to both
stdout and stderr, and then I'll use that as my generator:

imadev:/var/tmp/bash-4.0$ foo() { echo stdout; echo stderr >&2; }
imadev:/var/tmp/bash-4.0$ foo | tr '[:lower:]' '[:upper:]'
stderr
STDOUT
imadev:/var/tmp/bash-4.0$ foo |& tr '[:lower:]' '[:upper:]'
STDOUT
STDERR

I find that easier to work with.




reply via email to

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