[Techtalk] Joining commands together in bash

Almut Behrens almut-behrens at gmx.net
Tue May 13 20:31:54 EST 2003

On Tue, May 13, 2003 at 03:37:52PM +0100, Conor Daly wrote:
> On Tue, May 13, 2003 at 07:29:08PM +1000 or so it is rumoured hereabouts, 
> Rasjid Wilcox thought:
> > 
> > I think a > was lost in line wrap-around.  The original post had:
> > ... >&/dev/null
> > 
> > which means redirect all output (both output to stdout and stderr) to 
> > /dev/null.  The & in this case is not background execution, but says 'join 
> > the output from stdout and stderr together'. 
> Oh, I thought that was csh only.  The redirect syntax I use with bash is:
> command > /dev/null 2>&1

that's absolutely right, in principle, though bash is an exception in
that respect, in that it accepts both type of redirection idioms.

With the little test program "cmd"


  print STDOUT "Output to stdout\n";   # same as print "...\n", btw.
  print STDERR "Output to stderr\n";

you'd get _with bash_:

$ cmd
Output to stdout
Output to stderr

$ cmd >outfile
Output to stderr

$ cmd 2>outfile
Output to stdout

$ cmd >outfile 2>&1    # no terminal output, as expected

$ cmd >&outfile        # no terminal output, as expected

On the other hand, when you want to write cross-platform compatible
code (such as install and configure scripts) it's usually a good idea
to stick with the least common denominator...

The Bourne-shell compatible shells distributed with most traditional
Unixes do not support the ">&file" syntax -- for example:

$ cmd >&outfile
/bin/sh: outfile: bad file unit number

$ cmd >&outfile
/bin/sh: outfile: Generated or received a file descriptor number that is not valid.

$ cmd >&outfile
/bin/sh: outfile: bad file unit number

$ cmd >&outfile
outfile: bad number


More information about the Techtalk mailing list