如何使用 Bash 将标准输出和标准错误重定向并附加到文件中 [英] How to redirect and append both standard output and standard error to a file with Bash
本文介绍了如何使用 Bash 将标准输出和标准错误重定向并附加到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要将标准输出重定向到 Bash 中的截断文件,我知道使用:
To redirect standard output to a truncated file in Bash, I know to use:
cmd > file.txt
在 Bash 中重定向标准输出,附加到文件,我知道使用:
To redirect standard output in Bash, appending to a file, I know to use:
cmd >> file.txt
将标准输出和标准错误重定向到截断的文件,我知道使用:
To redirect both standard output and standard error to a truncated file, I know to use:
cmd &> file.txt
如何重定向标准输出和附加到文件的标准错误?cmd &>>file.txt
对我不起作用.
How do I redirect both standard output and standard error appending to a file? cmd &>> file.txt
did not work for me.
推荐答案
cmd >>file.txt 2>&1
Bash 从左到右执行重定向如下:
Bash executes the redirects from left to right as follows:
>>file.txt
:以追加模式打开file.txt
并将stdout
重定向到那里.2>&1
:将stderr
重定向到stdout
当前所在的位置".在这种情况下,这是一个以追加模式打开的文件.换句话说,&1
重用了stdout
当前使用的文件描述符.
>>file.txt
: Openfile.txt
in append mode and redirectstdout
there.2>&1
: Redirectstderr
to "wherestdout
is currently going". In this case, that is a file opened in append mode. In other words, the&1
reuses the file descriptor whichstdout
currently uses.
这篇关于如何使用 Bash 将标准输出和标准错误重定向并附加到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文