当您想将 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)