Thank to muchThanks @rouble for sharing sharing theiryour solution.
For those who need to intercept not only the correct exit code, but also the output of the grep command, here's what worked for me:
echo "before"
grep_exit_code=0
grep_output=$(echo "a" | grep "b") || grep_exit_code=$? ; if [[ $grep_exit_code -ne 1 ]]; then (exit $grep_exit_code); fi
echo "grep_exit_code=$grep_exit_code"
echo "grep_output=$grep_output"
echo "after"