Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
2  
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
add comment

1 Answer 1

up vote 9 down vote accepted

The if is just interested in the exit code of the grep (if 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
1  
You're right -- I did try it out, but misinterpreted the results. Answer retracted. –  Ansgar Esztermann Mar 19 '12 at 11:24
add comment

Your Answer

 
discard

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.