3 ms·
That's a valid concern, but in this case it won't cause problems -- grep exits with 0 if a line was selected; in the case of `grep -v grep` that means there was
by hddqsb 5y ago
That's a valid concern, but in this case it won't cause problems -- grep exits with 0 if a line was selected; in the case of `grep -v grep` that means there was a line without "grep", which is what we want.
(Also @thewakalix made a good suggestion to reverse the greps.)
- dylan604 5y agoexcept, your grep -v portion will always return a result so $? will also always return 0. even if your grep banana did not find anything. the reversing the greps will be my new default behavior
- hddqsb 5y agoNope, try it out: $ printf 'banana\ngrep banana\n' | grep banana | grep -v grep banana $ echo $? 0 $ printf 'grep banana\n' | grep banana | grep -v grep $ echo $? 1 To clarify my previous comment, `grep -v grep` exits with 0 if there was a line without "grep" in the output of `grep banana`.