">/dev/null 2>&1" in `if` statement

10,631

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:
10,631

Related videos on Youtube

mibzer
Author by

mibzer

Updated on September 18, 2022

Comments

  • mibzer
    mibzer almost 2 years

    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
    then 
    bla bla
    • Admin
      Admin over 12 years
      Was that really intended to be /dev/nul?
    • Admin
      Admin over 12 years
      Well it is codded like that. I didn't do that. What is your thoughts about it ?
    • Admin
      Admin over 12 years
      It's /dev/null on Linux, and I'm pretty sure every other modern *nix out there.
  • tiwari.vikash
    tiwari.vikash over 12 years
    You're right -- I did try it out, but misinterpreted the results. Answer retracted.