">/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 outputs2>&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.
Related videos on Youtube
Author by
mibzer
Updated on September 18, 2022Comments
-
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 anif
conditionif /usr/bin/egrep -e "$param1" -e "$param2" -e "param3" ${file} > /dev/null 2>&1 then bla bla
-
Admin over 12 yearsWas that really intended to be
/dev/nul
? -
Admin over 12 yearsWell it is codded like that. I didn't do that. What is your thoughts about it ?
-
Admin over 12 yearsIt's /dev/null on Linux, and I'm pretty sure every other modern *nix out there.
-
-
tiwari.vikash over 12 yearsYou're right -- I did try it out, but misinterpreted the results. Answer retracted.