小编fhi*_*iyo的帖子

为什么`command 1>file.txt 2>file.txt` 的行为与`command 1>file.txt 2>&1` 的行为不同?

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

shell bash io-redirection echo

21
推荐指数
2
解决办法
4144
查看次数

标签 统计

bash ×1

echo ×1

io-redirection ×1

shell ×1