Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Oftentimes I run into small bash scripts that use this sort of syntax in if statements:

some command > /dev/null 2>&1

What is the purpose of outputting to /dev/null like that, and what does the 2>&1 mean?

It always seems to work but I'd like to know what it's doing.

share|improve this question
up vote 39 down vote accepted

>/dev/null redirects the command standard output to the null device, which is a special device which discards the information written to it

2 >&1 redirects the standard error stream to the standard output stream (stderr = 2, stdout = 1).

In practice it prevents any output from the command (both stdout and stderr) from being displayed. It's used when you don't care about the command output.

share|improve this answer

STDIN is represented by 0, STDOUT by 1, and STDERR by 2.

/dev/null is the bit-bucket: the place where you dump anything you don't need.

So, the STDOUT is redirected to the bit-bucket(trash) and the STDERR is redirected to where the STDOUT is located: the bit-bucket.

You can also do this:

>/dev/null 2>/dev/null
share|improve this answer
Nice explanation. I also think there is a shortcut for the above: &> /dev/null – Chip Castle Aug 1 '13 at 20:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.