bash 如何将 stdout 和 stderr 都重定向到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7526971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:03:57  来源:igfitidea点击:

How to redirect both stdout and stderr to a file

bashstdoutstderr

提问by sdmythos_gr

I am running a bash script that creates a log file for the execution of the command

我正在运行一个 bash 脚本,它为命令的执行创建一个日志文件

I use the following

我使用以下

Command1 >> log_file
Command2 >> log_file

This only sends the standard output and not the standard error which appears on the terminal.

这仅发送标准输出而不是终端上出现的标准错误。

Can I log both the stderr and stdout logged to a file?

我可以将 stderr 和 stdout 都记录到一个文件中吗?

回答by Mat

If you want to log to the same file:

如果要登录到同一文件:

command1 >> log_file 2>&1

If you want different files:

如果你想要不同的文件:

command1 >> log_file 2>> err_file

回答by Costi Ciudatu

The simplest syntax to redirect both is:

重定向两者的最简单语法是:

command &> logfile

If you want to append to the file instead of overwrite:

如果要附加到文件而不是覆盖:

command &>> logfile

回答by Laurent Legrand

You can do it like that 2>&1:

你可以这样做 2>&1:

 command > file 2>&1

回答by blankabout

Use:

用:

command >>log_file 2>>log_file

回答by PaulDaviesC

Please use command 2>fileHere 2stands for file descriptor of stderr. You can also use 1instead of 2so that stdoutgets redirected to the 'file'

请使用command 2>fileHere2代表stderr 的文件描述符。您也可以使用1代替,2以便将标准输出重定向到“文件”