Search code examples
bashshellzshexit-code

Getting exit code of last shell command in another script


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.

For example:

sleep 100; my_notify

It would be nice to get the exit code of the long running script. The problem is that calling my_notify creates a new process that does not have access to the $? variable.

Compare:

~ $: ls nonexisting_file; echo "exit code: $?"; echo "PPID: $PPID"
ls: nonexisting_file: No such file or directory
exit code: 1
PPID: 6203

vs.

~ $: ls nonexisting_file; my_notify
ls: nonexisting_file: No such file or directory
exit code: 0
PPID: 6205

The my_notify script has the following in it:

#!/bin/sh
echo "exit code: $?"
echo "PPID: $PPID"

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 time, e.g. my_notify longrunning_command... 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.

Can this be done or is it fundamentally incompatible with the way that shells work?

My shell is Z shell (zsh), but I would like it to work with Bash as well.


Solution

  • You'd really need to use a shell function in order to accomplish that. For a simple script like that it should be pretty easy to have it working in both zsh and bash. Just place the following in a file:

    my_notify() {
      echo "exit code: $?"
      echo "PPID: $PPID"
    }
    

    Then source that file from your shell startup files. Although since that would be run from within your interactive shell, you may want to use $$ rather than $PPID.