`set -e` and `grep` idiom for preventing premature exit from shell script when pattern not found

You can put the grep in an if condition, or if you don't care about the exit status, add || true.

Example: grep kills the shell

$ bash
$ set -e
$ echo $$
33913
$ grep foo /etc/motd
$ echo $$
9233

solution 1: throw away the non-zero exit status

$ bash
$ set -e
$ echo $$
34074
$ grep foo /etc/motd || true
$ echo $$
34074

solution 2: explicitly test the exit status

$ if ! grep foo /etc/motd; then echo not found; fi
not found
$ echo $$
34074

From the bash man page discussing set -e:

The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or ││ list except the command following the final && or ││, any command in a pipeline but the last, or if the command’s return value is being inverted with !.