Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

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

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

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

cmd > file.txt

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

(为了重定向Bash中的stdout ,将其附加到文件中,我知道要使用:)

cmd >> file.txt

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

(要将stdoutstderr都重定向到截断的文件,我知道使用:)

cmd &> file.txt

How do I redirect both stdout and stderr appending to a file?

(如何将stdoutstderr都重定向到文件?)

cmd &>> file.txt did not work for me.

(cmd &>> file.txt对我不起作用。)

  ask by flybywire translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
cmd >>file.txt 2>&1

Bash executes the redirects from left to right as follows:

(Bash执行从左到右的重定向,如下所示:)

  1. >>file.txt : Open file.txt in append mode and redirect stdout there.

    (>>file.txt :以附加模式打开file.txt在那里重定向stdout 。)

  2. 2>&1 : Redirect stderr to "where stdout is currently going" .

    (2>&1 :将stderr重定向到stdout当前所在的位置” 。)

    In this case, that is a file opened in append mode.

    (在这种情况下,这是在附加模式下打开的文件。)

    In other words, the &1 reuses the file descriptor which stdout currently uses.

    (换句话说, &1重用了stdout当前使用的文件描述符。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...