[clug] bash stderr and not-so-stderr?

Nathan O'Sullivan nathan at mammoth.com.au
Thu Nov 27 00:50:14 GMT 2008


You need to think of it in terms of changing what each stream is going
to.

2>&1 doesnt merge the two streams, it just sets up stream 2 so it
outputs to whatever stream 1 is outputting at when its parsed, and
parsing is left to right.


Consider at a console, where stream 1 and 2 are both the console by
default.

echo hi there 2>/dev/null 1>&2

This prints nothing. First stream 2 is set to /dev/null, and then stream
1 is assigned to whatever stream 2 has (/dev/null). Finally the command
runs, which prints to stream 1 (/dev/null)


echo hi there 1>&2 2>/dev/null

This prints "hi there"; because first stream 1 output is set to whatever
stream 2 output is (ie, the console); then stream 2 is set to output
to /dev/null. Finally the command runs, which prints to stream 1 (the
console).

Regards
Nathan

On Mon, 2008-11-24 at 20:45 +1100, David Schoen wrote:
> Hmmm. I'm almost certain I've always done it the other way, which
> makes much more sense to me stream wise.
> 
> Anyway, that appears to have fixed it (will know for certain tomorrow).
> 
> - Dave.
> 
> 2008/11/24 Jeremy Kerr <jk at ozlabs.org>:
> > Hi David,
> >
> >> The problematic line looks something like:
> >>     /usr/bin/rdiff-backup $from $to 2>&1 >> $MAILFILE || exit 1
> >>
> >> I've simplified that line slightly for this post but it has the same
> >> problem.
> >
> > You need to redirect stderr to stdout *after* you've redirected stdout:
> >
> >  /usr/bin/rdiff-backup $from $to >>$MAILFILE 2>&1
> >
> > Cheers,
> >
> >
> > Jeremy
> >



More information about the linux mailing list