重定向 standard output到 Bash 中的截断文件,我知道要使用:
cmd > file.txt
要在 Bash 中重定向标准输出, append 到文件,我知道要使用:
cmd >> file.txt
重定向标准输出和standard error到截断的文件,我知道要使用:
cmd &> file.txt
如何重定向 append 到文件的标准输出和标准错误? cmd &>> file.txt
对我不起作用。
最佳答案
cmd >>file.txt 2>&1
Bash 从左到右执行重定向如下:
>>file.txt
:以追加模式打开file.txt
,并在那里重定向stdout
。2>&1
:将stderr
重定向到“stdout
当前所在的位置”。在这种情况下,这是一个以 append 模式打开的文件。换句话说,&1
重用了 stdout
当前使用的文件描述符。https://stackoverflow.com/questions/876239/