当您想将 stdout 和 stderr 重定向到同一个文件时,可以使用command 1>file.txt 2>&1
, 或command &>file.txt
。但是为什么command 1>file.txt 2>file.txt
与上面两个命令的行为不同呢?
以下是验证命令。
$ cat redirect.sh
#!/bin/bash
{ echo -e "output\noutput" && echo -e "error" 1>&2; } 1>file.txt 2>&1
{ echo -e "output\noutput" && echo -e "error" 1>&2; } 1>file1.txt 2>file1.txt
{ echo -e "error" 1>&2 && echo -e "output\noutput"; } 1>file2.txt 2>file2.txt
{ echo -e "output" && echo -e "error\nerror" 1>&2; } 1>file3.txt 2>file3.txt
{ echo -e "error\nerror" 1>&2 && echo -e "output"; } …
Run Code Online (Sandbox Code Playgroud) 当您 fork 一个进程时,子进程继承其父进程的文件描述符。我知道当发生这种情况时,子进程会收到一份父文件描述符表的副本,每个表中的指针都指向相同的打开文件描述。这是与文件表相同的东西,如http://en.wikipedia.org/wiki/File_descriptor,还是其他东西?