Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

I don't understand the below if condition. I know that /dev/null 2 > &1 is sending output to error stream (please correct me if I am wrong). But I don't get it when it is in an if condition

if /usr/bin/egrep -e "$param1" -e "$param2" -e "param3" ${file} > /dev/null 2>&1
bla bla
share|improve this question
Was that really intended to be /dev/nul? – Michael Kjörling Mar 19 '12 at 10:57
Well it is codded like that. I didn't do that. What is your thoughts about it ? – mibzer Mar 19 '12 at 11:47
It's /dev/null on Linux, and I'm pretty sure every other modern *nix out there. – Michael Kjörling Mar 19 '12 at 11:59
up vote 13 down vote accepted

The if is just interested in the exit code of the grep (whether it found some lines matching the conditions), but not the output of the grep command, so it redirects all output to /dev/null, which means nowhere.

This redirection happens in two parts:

  • > /dev/null redirects standard output to nowhere, i.e. the lines that egrep usually outputs
  • 2>&1 redirects the error output also to the same location as the standard output, i.e. to /dev/null; this is for suppressing errors messages from egrep.
share|improve this answer
You're right -- I did try it out, but misinterpreted the results. Answer retracted. – Ansgar Esztermann Mar 19 '12 at 11:24

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.