title: What is the proper way to detect shell exit code when errexit option is set?
body: <p>I prefer to <strong>write solid shell</strong> code, so the errexit &amp; nounset is always set.</p>
  <p>The following code will stop at bad_command line</p>
  <pre><code>#!/bin/bash
  set -o errexit
  set -o nounset
  bad_command # stop here
  good_command
  </code></pre>
  <p>I want to capture it, here is my method</p>
  <pre><code>#!/bin/bash
  set -o errexit
  set -o nounset
  rc=1
  bad_command &amp;&amp; rc=0 # stop here
  [ $rc -ne 0 ] &amp;&amp; do_err_handle
  good_command
  </code></pre>
  <p>Is there any better or cleaner method</p>
  <p>My Answer:</p>
  <pre><code>#!/bin/bash
  set -o errexit
  set -o nounset
  if ! bad_command ; then
    # error handle here
  fi
  good_command
  </code></pre>