title: Getting exit code of last shell command in another script
body: <p>I am trying to beef up my notify script. The way the script works is that I put it behind a long running shell command and then all sorts of notifications get invoked after the long running script finished.</p>
  <p>For example:</p>
  <pre><code>sleep 100; my_notify
  </code></pre>
  <p>It would be nice to get the exit code of the long running script. The problem is that calling <code>my_notify</code> creates a new process that does not have access to the <code>$?</code> variable.</p>
  <p>Compare:</p>
  <pre><code>~ $: ls nonexisting_file; echo &quot;exit code: $?&quot;; echo &quot;PPID: $PPID&quot;
  ls: nonexisting_file: No such file or directory
  exit code: 1
  PPID: 6203
  </code></pre>
  <p>vs.</p>
  <pre><code>~ $: ls nonexisting_file; my_notify
  ls: nonexisting_file: No such file or directory
  exit code: 0
  PPID: 6205
  </code></pre>
  <p>The <code>my_notify</code> script has the following in it:</p>
  <pre><code>#!/bin/sh
  echo &quot;exit code: $?&quot;
  echo &quot;PPID: $PPID&quot;
  </code></pre>
  <p>I am looking for a way to get the exit code of the previous command without changing the structure of the command too much. I am aware of the fact that if I change it to work more like <code>time</code>, e.g. <code>my_notify longrunning_command...</code> my problem would be solved, but I actually like that I can tack it at the end of a command and I fear complications of this second solution.</p>
  <p>Can this be done or is it fundamentally incompatible with the way that shells work?</p>
  <p>My shell is Z shell (<code>zsh</code>), but I would like it to work with Bash as well.</p>