Yes, there is a better way—or, at any rate, there is a more idiomatic, terser, less error-prone way.
#!/bin/sh -e
# [...]
command-that-is-allowed-to-fail || true
# [...]
See how that works? If command-that-is-allowed-to-fail
fails, the ||
operator passes control to the true
builtin. The latter does nothing except this: it never fails.
Incidentally, there exists a false
command as well, which does nothing except this: it always fails. However, in some shells (like Dash) false
is not a builtin, so you might in some circumstances have to invoke it as /bin/false
or the like.
NOTE
A mentor taught me this technique about 2004. I doubt that I would have discovered the technique on my own, for it was nonobvious to me. The technique is simple and appealing, though, once one has become acquainted with it. Since learning the technique, I have used it extensively. You can, too.