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
  • 2
    Was that really intended to be /dev/nul? – a CVn 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. – a CVn Mar 19 '12 at 11:59

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.
  • 1
    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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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