如何使用Bash将stdout和stderr重定向并附加到文件? [英] How to redirect and append both stdout and stderr to a file with Bash?

查看:154
本文介绍了如何使用Bash将stdout和stderr重定向并附加到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要将 stdout 重定向到Bash中的截断文件,我知道使用:

To redirect stdout to a truncated file in Bash, I know to use:

cmd > file.txt

要重定向Bash中的 stdout 并将其附加到文件中,我知道可以使用:

To redirect stdout in Bash, appending to a file, I know to use:

cmd >> file.txt

要将 stdout stderr 都重定向到截断的文件,我知道使用:

To redirect both stdout and stderr to a truncated file, I know to use:

cmd &> file.txt

如何将添加到文件的 stdout stderr 都重定向? cmd &>> file.txt对我不起作用.

How do I redirect both stdout and stderr 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:

  1. >>file.txt:以附加模式打开file.txt并在其中重定向stdout.
  2. 2>&1:将stderr重定向到"stdout当前正在运行的地方" .在这种情况下,即是在追加模式下打开的文件.换句话说,&1重用了stdout当前使用的文件描述符.
  1. >>file.txt: Open file.txt in append mode and redirect stdout there.
  2. 2>&1: Redirect stderr to "where stdout is currently going". In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout currently uses.

这篇关于如何使用Bash将stdout和stderr重定向并附加到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆