LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   what does "standard error was duplicated as standard output" mean? (http://www.linuxquestions.org/questions/linux-general-1/what-does-standard-error-was-duplicated-as-standard-output-mean-482662/)

nickleus 09-12-2006 02:48 AM

what does "standard error was duplicated as standard output" mean?
 
here:
http://www.gnu.org/software/bash/man...ref.html#SEC37
it says:
Code:

ls 2>&1 > dirlist
Quote:

directs only the standard output to file dirlist, because the standard error was duplicated as standard output before the standard output was redirected to dirlist.
i know that in this example the standard output is sent to the file dirlist and the standard error is written to the screen. but what does it really mean that "the standard error was duplicated as standard output"?

Samotnik 09-12-2006 03:16 AM

It means that error messages was send to standard error and standard output simultaneosly

nickleus 09-14-2006 02:50 PM

ok, so that means that standard error messages were printed to the screen first (or at least directed to the screen, in the case that there were any errors), then the standard output (if there was any) would be directed to the file? maybe this is a dumb question, but can you ever get standard errors and output at the same time? or is it always one or the other?

haertig 09-14-2006 03:44 PM

Quote:

ls 2>&1 > dirlist
Let's break it down.

"2" is "stderr" a.k.a. "standard error"
"1" is "stdout" a.k.a. "standard output"
">" can be thought of as "sent to"
"&" can be thought of as "the same place as"

By default, "1" is normally sent to your screen. So is "2".

To send "standard output" to a file named "dirlist", you would do this: 1>dirlist Read this as:
Code:

"standard output" + "sent to" + "dirlist"
You can leave off the leading "1" and it means the same thing, since "1" is assumed if nothing is specified. Therefore, >dirlist is just a shorthand way of writing 1>dirlist

Looking at the text string "2>&1", this can be read as:
Code:

"standard error" + "sent to" + "the same place as" + "standard output"
Now, order is important. ls 2>&1 >dirlist is not the same as ls >dirlist 2>&1
The first case directs 2 into the same place as 1, and then directs 1 into dirlist. But at the time 2 was sent to the same place as 1, 1 was going to it's default. Your screen. But 2 goes to your screen by default too! So that operation didn't really do anything.

In the second case, 1 was directed to dirlist first, and then 2 was directed to the same place as 1. So 2 would then go to dirlist also.

So to capture both stdout and stderr to a file, first direct 1 to the file, then direct 2 to the same place as 1. e.g., ls >dirlist 2>&1

nickleus 09-15-2006 06:14 AM

ok. got it. thank you very much haertig!
what i didn't see at first was the second ">":
Code:

ls 2>&1 >dirlist
is the same as:
Code:

ls 2>&1 1>dirlist
totally clear now =)


All times are GMT -5. The time now is 11:21 AM.