[CLUE-Tech] bash: overwrite and redirect

marcus hall marcus at tuells.org
Tue Feb 18 09:57:53 MST 2003


On Mon, Feb 17, 2003 at 10:42:50PM -0600, Michael J. Hammel wrote:
> > What's a straightforward way to both redirect and force a clobber?

> some command 2>&1 > result_file

> Redirects stderr to stdout, then redirects stdout to result_file,
> overwriting the existing contents of that file.

Careful about the order..  There is a subtle ordering to when each
argument is processed.  The above will first dup stdout to stderr,
*then* point stdout to result_file.  So, stderr will be going to
the old destination of stdout and only stdout will be going to the
file.  You really need to say:

some command > result_file 2>&1

This will redirect stdout to result_file, *then* dup stdout (now
pointing to result_file) to stderr.

Now, to further complicate things, in a pipeline, the pipe is
built *before* the arguments are processed, so for the sequence:

some command 2>&1 | another command

Before the arguments are processed for "some command", its stdout
is hooked to the pipe, so the "2>&1" dups stdout (hooked to the pipe)
to stderr.

marcus hall
marcus at tuells.org



More information about the clue-tech mailing list