I have a script like this:
#!/bin/sh
echo "hello"
echo "goodbye"
exit 1
When I run it on its own, I get the failed exit code as I expect.
$ ./fail.sh
hello
goodbye
$ echo $?
1
However, when I run it through grep -v
, the exit status changes to success:
$ ./fail.sh | grep -v hello
goodbye
$ echo $?
0
Is there a way to pipe a command's output into grep -v
and still have the status code be properly propagated? Of course in the real world the point of this would be to filter the output of a noisy command, while still detecting if the command failed.