n<&- 表示将 n 号输入关闭 <&- 表示关闭标准输入(键盘) n>&- 表示将 n 号输出关闭 >&- 表示将标准输出关闭
If you type >1,The shell will think take this as a file named 1 So you want to type &1 to distinguish. So 2>&1 means redirect stderr to stdout. You redirect stdout to fileanme, so that all stderr and stdout will redirect to filename.
代码:
stderr -> stdout -> filename.
Of cause you can redirect stdout and stderr like this: command &>filename. But this is not so good. You will see that the messages from stderr and stdout may cross into each other. Why? Caz stderr is non-bufferred output, while stdout is bufferred output. So type command>filename 2 >&1 will be better. But if you type command>filename 1 >&2 will get the some effect as command &>filename
代码:
We assume the x corrdinate is time zone
When we type command &>filename, the stream is like this
Caz, non-buffered output is slow.
V y
|
| s t d e r r
| stdout
|
| x
|---------------------------------------------------------->(time)
So you see that stderr and stdout cross into each other.
But while you type like command>filename 2 >&1
the stream is like this,Caz, buffered output is fast.
|
| stderr
| stdout
|
| x
|---------------------------------------------------------->(time)
So this is good.