LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell short question(stdout, stderr) (https://www.linuxquestions.org/questions/programming-9/shell-short-question-stdout-stderr-253507/)

blackzone 11-11-2004 12:52 AM

shell short question(stdout, stderr)
 
I know:
command >/dev/null //output not shown
command 1>/dev/null //standard output not shown
command 2>/dev/null //standard error not shown
command 2&> /dev/null //standard output + error not shown

how about:
command 1>/dev/null 2>&1

it sends stdout to /dev/null and??

Hko 11-11-2004 02:47 AM

Quote:

how about:
command 1>/dev/null 2>&1

it sends stdout to /dev/null and??
...sends stdandard error (stderr) to the same place where standard input (stdin) is going.

Note that the order matters: "command 2>&1 >/dev/null" doesn't work because stderr is first redirected to the same place as stdout, after that, stdout is redirected to /dev/null.

In the case of redirecting to /dev/null it does not matter much which form you use, but when you want to redirect both stdout and stderr to a single file, this doesn't work: "command >my_file.txt 2>my_file.txt"
but this does: "command >my_file.txt 2>&1"

These forms are exactly the same, only the syntax is different:

command >some_file 2>&1
command 1>some_file 2>&1
command &> some_file
command >& some_file

And I suppose the one you mentioned ("command 2&> some_file") belongs to this list as well.

perfect_circle 11-11-2004 03:12 AM

Quote:

...sends stdandard error (stderr) to the same place where standard input (stdin) is going.
WRONG!!!
2>&1 redirects stderr to stdout, error messages get sent to the same place as standard output.

this is why
Code:

command 1>/dev/null 2>&1
is the same with
Code:

command &>/dev/null
.
Both redirect stderr and stdout to /dev/null

jschiwal 11-11-2004 07:39 AM

It redirects stderr to stdout which is redirected to /dev/null. The effect is the same as
2&>/dev/null.

Hko 11-11-2004 09:01 AM

Quote:

Originally posted by perfect_circle
WRONG!!!
2>&1 redirects stderr to stdout, error messages get sent to the same place as standard output.
TRUE,
I said by mistake: "standard input(stdin)", where it should have been: "standard output (stdout).

Thanks for correcting that.


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