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?

  • 3
    Wouldn't it be very easy to just try whether the result is the same? And much faster than asking here? Questions which do not even show a tiny bit of research effort are less welcome here. – Hauke Laging May 15 '14 at 14:08
  • 1
    @HaukeLaging: It's still a useful question. – Brian May 15 '14 at 17:51
  • @staticx So what? – Hauke Laging May 15 '14 at 20:50
  • @Hauke: so there is nothing wrong with it – Brian May 15 '14 at 21:27
  • 1
    @staticx As you ignore the obvious I point you to the official assessment: unix.stackexchange.com/help/how-to-ask And it certainly doesn't mean "Research or be on-topic" – Hauke Laging May 15 '14 at 22:41

Yes, there is a difference.

/home/user/script.sh >> /home/user/stdout_and_error.log  2>&1

This will send both STDOUT and STDERR to /home/user/stdout_and_error.log.

/home/user/script.sh 2>&1 >> /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.


On the first command line, the shell sees >> file first and append stdout to file . Next 2>&1 sends fd2 ( stderr ) to the same place fd1 is going - that's to the file. And that's what you want.

On the second command line, the shell sees 2>&1 first. That means "make the standard error (file descriptor 2) go to the same place as the standard output (fd1) is going." There's no effect because both fd2 and fd1 are already going to the terminal. Then >> file appends fd1 ( stdout ) to file . But fd2 ( stderr ) is still going to the terminal.


>> Appending stdout (stream #1) to a file.

2>&1 Combining stderr (stream #2) with stdout (stream #1) (Adds stderr into stdout)

> Writes stdout (stream #1) to a file, overwriting the file.

1> Writes stdout (stream #1) to a file, overwriting the file. Same as above.

2>Writes stderr (stream #2) to a file, overwriting the file.


You're first example would append stdout to a file, then add stderr to stdout.

You're second example would add stderr to stdout, then append the combined stdout (with stderr included) to a file.

  • 3
    No, the second example does not send the stderr to file, stderr went to file descrpitor 1. – cuonglm May 15 '14 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.