what does > /dev/null 2>&1 mean ?

Categories: bash

Ever been confused seeing those funny greater than signs being used on a unix system ? Let me try to explain those weird commands.

The greater than sign means the output is being redirected somewhere else. Let’s have a look at an example to be more clear.

wget ${url} > /dev/null 2>&1

In this example, something is being redirected to /dev/null and something else is being redirected to &1.

Before moving further, lets discuss about STDIN,STDOUT, and STDERR. These are the 3 sources of input and output for any particular program. STDIN means Standard Input, STDOUT means Standard Output, and STDERR means Standard Error. They are numerically denoted as 0, 1 and 2 respectively. If you do not name or number any of them, that particularly means you are talking about STDOUT.

Getting back to the example,

  • > /dev/null means that STDOUT is being redirected to /dev/null. It is known as bit-bucket and everything being written there is discarded automatically.
  • 2>&1 means 2 (i.e STDERR) is being redirected to &1 (i.e where the STDOUT is going. In this case /dev/null ).
  • That means, using > /dev/null 2>&1 can be used to make any program work quietly with no STDOUT and/or STDERR.

    This can also be done alternatively as

     >/dev/null 2>/dev/null

    It’s still the same.

    No Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>