I would like a brief explanation of the following command line:

grep -i 'abc' content 2>/dev/null 
up vote 241 down vote accepted

The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.

If you don't specify a number then the standard output stream is assumed but you can also redirect errors

> file redirects stdout to file
1> file redirects stdout to file
2> file redirects stderr to file
&> file redirects stdout and stderr to file

/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.

  • 1
    is there a difference between > /dev/null 2>&1 and &> /dev/null – Alexander Mills Oct 19 '17 at 0:25
  • 6
    In practice today I don't think there is 2>&1 is an older syntax so &> would not have worked years ago but both are equivalent. – Warren Hill Oct 19 '17 at 2:47

In short, it redirects stderr (fd 2) to the black hole (discards the output of the command).

Some more common use cases for redirection:

command > /dev/null 2>&1 &

Run command in the background, discard stdout and stderr

command >> /path/to/log 2>&1 &

Run command and append stdout and stderr to a log file.

  • 2
    Is there a good reason to use > /dev/null 2>&1 instead of &> /dev/null? – Craig McQueen Nov 30 '15 at 6:43
  • 6
    @CraigMcQueen &> is new in Bash 4, the former is just the traditional way, I am just so used to it (easy to remember). – Terry Wang Nov 30 '15 at 12:24

/dev/null is treated as black hole in Linux/Unix, so you can put anything into this but you will not be able to get it back from /dev/null.

Further, 2> means that you are redirecting (i.e. >) the stderr (i.e. 2) into the black hole (i.e. /dev/null)

Your command is:

grep -i 'abc' content 2>/dev/null 

Don't try to end with another forward slash like this - 2>/dev/null/ (it's not a directory).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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