Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 its doing.

share|improve this question

2 Answers 2

up vote 20 down vote accepted

>/dev/null = redirect the command standard output to the null device, it's a special device which discards the information written to it

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

In practice it prevents any ouput 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
2  
Nice explanation. I also think there is a shortcut for the above: &> /dev/null –  Chip Castle Aug 1 '13 at 20:48

Your Answer

 
discard

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.