27.11. Is It "2>&1 file" or "> file 2>&1"? Why?
One of the common questions about
Bourne-type shells is why only the second command shown below will
redirect both stdout and stderr
( Section 43.1) to a file:
$ cat food 2>&1 >file
cat: can't open food
$ cat food >file 2>&1
$
Although
some manual pages don't mention this, the shell
processes I/O redirections from left to right:
-
On the first command line, the shell sees
2>&1 first. That means
"make the standard error (file descriptor 2) go to
the same place that the standard output (fd1) is
going." There's no effect because
both fd2 and fd1 are already going to the terminal. Then
>file redirects fd1
(stdout) to file. But fd2
(stderr) is still going to the terminal.
-
On the second command line, the shell sees
>file first and redirects
stdout to file. Next
2>&1 sends fd2
(stderr) to the same place fd1 is
going -- that's to the file. And
that's what you want.
Section 36.16 has much more about the
m>&n
operator.
-- JP
| | | 27.10. Which One Will the C Shell Use? | | 27.12. Bourne Shell Quoting |
Copyright © 2003 O'Reilly & Associates. All rights reserved.
|