title: Exit hook working both on Bash and ZSH
body: <p>I need to create a script that basically sets some env variables, starts a subshell and, when exited, run some additional code, as a hook. It needs to work both on Bash and ZSH, and I'm aware that maybe I should maintain two different scripts.</p>
  
  <p>The reason I need it is to create some kind of <em>bootstrap</em> script. For example, I create a file named <em>Bootstrap.sh</em> with two functions, <code>start</code> and <code>exit</code>, and when I run the script, <code>start</code> should be called and a new subshell is activated. When the subshell exits, <code>exit</code>, is finally called. For example:</p>
  
  <pre><code># Bootstrap.sh
  start() {
      export CUSTOM_VAR="hello world"
  }
  
  exit() {
      echo "byebye"
  }
  </code></pre>
  
  <blockquote>
    <p>$ bootstrap<br>
    $ # finds "Bootstrap.sh", loads it and then calls "start" function<br>
    (myenv) $ echo $CUSTOM_VAR<br>
    hello world<br>
    (myenv) $ exit<br>
    byebye  # this is my exit hook<br>
    $ # back to "normal" shell</p>
  </blockquote>
  
  <p>I'm testing hooks with <a href="https://gist.github.com/lucassmagal/6226951" rel="nofollow">this script</a>, based on a suggestion to improve Python's <a href="https://gist.github.com/datagrok/2199506" rel="nofollow">virtualenv</a>. In this file I tried to use trap (a) and TRAPEXIT(b), none of those worked:</p>
  
  <pre><code># (a), the "bash way", I think
  my_hook() {
      echo "exit hook called"
  }
  trap my_hook EXIT
  exec "${@:-$SHELL}"
  </code></pre>
  
  
  
  <pre><code># (b), as explained in ZSH documentation
  # http://zsh.sourceforge.net/Doc/Release/Functions.html
  TRAPEXIT() {
      echo "exit hook called"
  }
  exec "${@:-$SHELL}"
  </code></pre>
  
  <p>So, how can I do it (in both ZSH and Bash, if possible)?</p>