How to get grep exit code but print all lines?
Use tee
and redirect it to stderr
my_command | tee /dev/stderr | grep -q '^Error'
It will save grep
exit status and duplicate all the output to stderr which is visible in console. If you need it in stdout you can redirect it there later like this:
( my_command | tee /dev/stderr | grep -q '^Error' ) 2>&1
Note that grep
will output nothing, but it will be on tee
.
You can do this with AWK:
command 2>&1 | awk '/^Error/{exit_code=1;}/^/ END{ exit !exit_code}'
This will print all the output lines and return 0 if it finds Error
and return 1 if it doesn't.