1

I saw in linux script there was a command argument > /dev/null 2>&1, I know it is to redirect the output to null, means silencing it. I also know about the numbering for 0,1,2 (STDIN, STDOUT, STDERR), but I don't get why need to have this line?

2>&1

Basically I want to know what is the difference between

>/dev/null

and

>/dev/null 2>&1
8

2>&1 will redirect stderr to wherever stdout currently points to. The argument >/dev/null will redirect stdout to /dev/null i.e discard/silent the output by command. But if you also want to discard (make silent) the stderr, then after redirecting stdout to /dev/null, specify 2>&1 to redirect stderr to the same place.

Example (For visualizing difference):

$ ls
file1
file2

$ ls file1 > /dev/null
$

Here the output of ls file1 is file1 which is sent to /dev/null and hence we get nothing. But:

$ ls file12 > /dev/null
ls: cannot access file12: No such file or directory

which gives stderr and as only output is sent to /dev/null. So, If you want to discard/silent stderr also then you can redirect stderr to stdout and hence both will be sent to /dev/null as follows:

$ ls file12 >/dev/null 2>&1
$

Note that the order/sequence of redirection matters. 2>&1 to redirect standard error must always be placed after redirecting standard output or it doesn't do anything. In above example if you run ls file12 2>&1 >/dev/null you will see the stderr printed to the terminal; if you run ls file12 >/dev/null 2>&1 you won't.

  • Alternatively You could also use ls file1 file12 2>/dev/null 1>&2 with the same effect—which first redirects stderr to /dev/null and then redirects stdout to point to the same place stderr is currently pointing to.
  • With the new version of bash you can also use >& simply like: ls file12 >& /dev/null which will redirects both stdout & stderr to /dev/null
  • Huh, I did not know that >& works the same as &>. Learn something new every few minutes. :) – Wildcard Mar 4 '16 at 9:43
  • It would be nice to know which version of bash started supporting >&. – Sarneet Kaur Aug 29 '17 at 18:41
2

someprogram > /dev/null 2>&1 works by redirecting the standard output (&1) to /dev/null, and then redirects standard error (&2) to the same place that &1 has been redirected to. If you do not also redirect stderr, then while standard output will be eliminated, standard error will still be sent to the terminal.

  • is 2>&1 executed first before someprogram > /dev/null? – hades Mar 4 '16 at 4:01
  • It's not executed as it's not a program; it redirects output from the program that you are executing. someprogram > /dev/null can actually be rewritten as someprogram 1> /dev/null. Rather than using 2>&1, you can also use someprogram 1> /dev/null 2> /dev/null, but Unix administrators are lazy, and that's more typing. – DopeGhoti Mar 4 '16 at 4:28
  • 2
    with the bash shell, the reason use 2>&1 instead of /dev/null 2> /dev/null is to prevent having two independent FDs working on the same destination or source. This process is called duplicating FDs. In the case of sending output to /dev/null you wont notice the difference, but if you were redirecting to a log the two FD's could clobber each others output. – the_velour_fog Mar 4 '16 at 5:50
  • 1
    @DopeGhoti I think 3172596 is actually asking if this redirection is processed from last to first. POSIX "Shell Command Language" sec 2.7 says "the order of evaluation is from beginning to end". Just look at it as something like 'so we pointed &1 to /dev/null, now pointing &2 to &1 will naturally point us there too.' – Arthur2e5 Mar 4 '16 at 9:41

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.