The issue I have with most, and perhaps all the answers here, is that they are losing the true exit code ($?) and mostly returning $? as 0. There is no way to differentiate if there was a match (exit code 0) or no match (exit code 1). This solution addresses that concern:
echo "anything" | grep e || exit_code=$?; if [[ $exit_code -ne 1 ]]; then (exit $exit_code); fi
grep's exit status is either 0, 1 or 2:
0 means there was a match
1 means there was no match
2 means an error occurred
Now, with the above solution:
test is not run and returns the true $? exit code of 0, and also sets
test is run and returns a fake $? exit code of 0, but we also sets
$exit_code=1 which can be used by future code to differentiate a match vs no match.
grep returns any other value, the
test is run and returns the true greater than 1 error exit code that should correctly halt the script, we also set
$exit_code appropriately, but that won't matter because the script will halt.