Is there a difference between those two lines ?
/home/user/script.sh >> /home/user/stdout_and_error.log 2>&1
/home/user/script.sh 2>&1 >> /home/user/stdout_and_error.log
knowing that I would like to put the stdout and execution errors of the script in the log file.
If there are no differences, what if I would like to log the logging itself?
Best Answer
Yes, there is a difference.
This will send both STDOUT and STDERR to
/home/user/stdout_and_error.log
.This will send STDOUT to
/home/user/stdout_and_error.log
, and STDERR to what was previously STDOUT.When you perform a shell redirection, the left side of the redirection goes to where the right side of the redirection currently goes. Meaning in
2>&1
, it sends STDERR (2) to wherever STDOUT (1) currently goes.But if you afterwards redirect STDOUT somewhere else, STDERR doesn't go with it. It continues to go wherever STDOUT was previously going. This is why in your first example, both STDOUT and STDERR will go to the same place, but in the second they won't.