title: How to run a command before a Bash script exits?
body: <p>If a Bash script has <code>set -e</code>, and a command in the script returns an error, how can I do some cleanup before the script exits?</p>
  
  <p>For example:</p>
  
  <pre><code>#!/bin/bash
  set -e
  mkdir /tmp/foo
  # ... do stuff ...
  rm -r /tmp/foo
  </code></pre>
  
  <p>How can I ensure that <code>/tmp/foo</code> is removed, even if one of the commands in <code>... do stuff ...</code> fails?</p>