How to redirect both stdout and stderr to a file [duplicate]

穿精又带淫゛_ 提交于 2019-11-26 03:29:05

问题


This question already has an answer here:

  • How to redirect and append both stdout and stderr to a file with Bash? 7 answers

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

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?


回答1:


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



回答2:


The simplest syntax to redirect both is:

command &> logfile

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

command &>> logfile



回答3:


You can do it like that 2>&1:

 command > file 2>&1



回答4:


Use:

command >>log_file 2>>log_file



回答5:


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



来源:https://stackoverflow.com/questions/7526971/how-to-redirect-both-stdout-and-stderr-to-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!