Why we need to have 2>&1 in /dev/null 2>&1?

23,332

Solution 1

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

Solution 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.

Share:
23,332

Related videos on Youtube

hades
Author by

hades

Updated on September 18, 2022

Comments

  • hades
    hades over 1 year

    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
    
  • Pankaj Goyal
    Pankaj Goyal about 8 years
    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.
  • the_velour_fog
    the_velour_fog about 8 years
    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.
  • Mingye Wang
    Mingye Wang about 8 years
    @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.'
  • Wildcard
    Wildcard about 8 years
    Huh, I did not know that >& works the same as &>. Learn something new every few minutes. :)
  • Sarneet Kaur
    Sarneet Kaur over 6 years
    It would be nice to know which version of bash started supporting >&.