Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion with the redirection of output to 2>&1?

I have checked couple of relevant posts regarding this in stackoverflow and other sources regarding the usage of 2>&1.

Unfortunately so far have not get my head around it completely.

I understand that 2 is the stderr and 1 is the stdout and we are combining with the 2>&1.

But my question is what is difference between:

1. mycommand > /dev/null       
2. mycommand 2> /dev/null      
3. mycommand > /dev/null 2>&1  

I was thinking:

  1. will redirect stdout and stderr to /dev/null
  2. will redirect stderr to /dev/null
  3. will redirect stdout and stderr to /dev/null

Relevant posts:

  • What does "/dev/null" mean at the end of shell commands)

  • i/o stream redirection on linux shell. how does the shell process a command with redirection?

  • What does “> /dev/null 2>&1″ mean? (http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/)

like image 378
Exploring Avatar asked Dec 10 '13 12:12

Exploring


People also ask

Why do we use 2 >> redirection?

which writes the output from one file to the input of another file. 2>&1 means that STDERR redirects to the target of STDOUT (which is the file dirlist) We are redirecting error output to standard output which in turn is being re-directed to file dirlist. Hence, both the output is written to file dirlist.

What does 2 &1 mean in shell script?

The expression 2>&1 copies file descriptor 1 to location 2 , so any output written to 2 ("standard error") in the execution environment goes to the same file originally described by 1 ("standard output").

What does 2 &1 at the end of a command do?

2>&1 means redirect the stderr ( >2 ) to where stdout is being redirected to ( &1 ). We won't cover stdin in this article, but we will talk about stdout and stderr .

What is redirection explain with example?

Redirection can be defined as changing the way from where commands read input to where commands send output. You can redirect the input and output of a command. For redirection, metacharacters are used.


3 Answers

See this:

mycommand > /dev/null

it will redirect channel 1 (which is stdout) of mycommand to /dev/null

mycommand 2> /dev/null

it will redirect channel 2 (which is stderr) to /dev/null

mycommand > /dev/null 2>&1

it will redirect channel 1 to /dev/null and then bind channel 2 (stderr) to channel 1 (stdout). Both will go into /dev/null

There is another one (just to complete)

mycommand 2>&1 > /dev/null

In this second case, I bind (the child's) stderr to stdout (of the parent) and then I find the child's stdout to /dev/null. The result is that you now get the child's stderr output on stdout and the stdout goes to the file. This is useful for processing stderr in a pipe, for example. (see this answer)

like image 61
chaos Avatar answered Sep 27 '22 23:09

chaos


(errfile doesn't exist)

$ cat errfile
cat: 0652-050 Cannot open errfile.

$ cat errfile > /tmp/stream.out
cat: 0652-050 Cannot open errfile.


$ cat errfile > /tmp/stream.out 2>&1

$ cat /tmp/stream.out
cat: 0652-050 Cannot open errfile.

($ rm /tmp/stream.out)

$ cat errfile 2>&1 > /tmp/stream.out
cat: 0652-050 Cannot open errfile.

$ cat /tmp/stream.out

$

Order is thus important and 2>&1 1>out is different than 1>out 2>&1 due to stream redirection at shell interpretation. You shoud redirect in "reverse" order. stdout > final than source > stdout

like image 36
NeronLeVelu Avatar answered Sep 28 '22 00:09

NeronLeVelu


Try these to get the differences:

echo "stderr" > /dev/fd/2 | >/dev/null
stderr
echo "stdout" > /dev/fd/1 | >/dev/null

both commands redirected to /dev/null but in first one we're writing to stderr which prints stderr but in second one it prints nothing

like image 38
vahid abdi Avatar answered Sep 27 '22 22:09

vahid abdi