title: Capturing exit code from sudo-run process
body: <p>I have a script in which I'm meticulously checking return codes for error conditions, so that I can abort early in the event of a failure. One step of this script involves running a command as the <code>root</code> user on another box, via <code>ssh</code> and <code>sudo</code>.</p>
  <p>Consider:</p>
  <pre><code>ssh $HOST sudo $CMD
  echo $?
  </code></pre>
  <p><code>ssh</code> passes return codes back just fine, but even if <code>$CMD</code> returns a nonzero exit code, <code>sudo</code> still returns 0 after running the command.</p>
  <p>How do I capture the return code to <code>$CMD</code>? I'm very partial to its being passed back as <code>ssh</code>'s return code, but if there's another simple method which can't be confused by the output of <code>$CMD</code>, I'm all ears.</p>