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.
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
>>
and not >
? –
Asta >>
appends to the file, >
overwrites. Search for "shell redirection" for more details. –
Ait make
vomits ten thousand errors that scroll off the screen: vi <(make 2>&1)
. –
Unscientific command 2>&1 | tee log_file
–
Ehlers >&
is a single "token" here. See the "Duplicating File Descriptors" part of the bash man page (or the whole REDIRECTION section). –
Ait time echo hi >> e.log 2>&1
the output of time
to stderr ends up in the terminal and not the file. –
Bogle 2>&1
can be metaphorically understood as an assignment stderr_handle = dup(stdout_handle);
in a C-like programming language. –
Sooty The simplest syntax to redirect both is:
command &> logfile
If you want to append to the file instead of overwrite:
command &>> logfile
|&
. See the comments on this question. –
Keaton &>
now works as expected on OS X 10.11.1 (seems to be bash 3.2), just for the record. –
Coruscation bash
matches the conciseness of the old command >& logfile
in tcsh
. It might almost be time for me to switch shells now! –
Turin You can do it like that 2>&1:
command > file 2>&1
2>&1
redirects file descriptor 2 (stderr) to file descriptor 1 (stdout) –
Palanquin &
means file descriptor. How would write to a file named 1
otherwise? –
Backer &
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" –
Hilariahilario 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'
© 2022 - 2024 — McMap. All rights reserved.
2>&1
needs to occur after>> log_file
. – Sooty