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.
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>&1
needs to occur after >> log_file
.
Jan 2, 2014 at 6:34
>>
appends to the file, >
overwrites. Search for "shell redirection" for more details.
make
vomits ten thousand errors that scroll off the screen: vi <(make 2>&1)
.
Mar 1, 2017 at 20:06
command 2>&1 | tee log_file
Aug 25, 2017 at 21:31
The simplest syntax to redirect both is:
command &> logfile
If you want to append to the file instead of overwrite:
command &>> logfile
&>
now works as expected on OS X 10.11.1 (seems to be bash 3.2), just for the record.
Nov 28, 2015 at 20:32
You can do it like that 2>&1:
command > file 2>&1
2>&1
redirects file descriptor 2 (stderr) to file descriptor 1 (stdout)
&
means file descriptor. How would write to a file named 1
otherwise?
Apr 27, 2020 at 19:56
&
to denote file descriptor because in this context only file descriptor is possible so &
is implicit. After ">" the destination can be anything, so we have to use explicit &
to specify that we are referring to a file descriptor rather than a file named "1"
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'