Search code examples
bashexit-codebash-trap

Propagating exit code to caller in case of a shell error from script having an exit trap


Is it possible to propagate an exit code to the caller in case of a syntax error in a Bash script with an EXIT trap? For example, if I have:

#! /bin/bash

set -eu

trap "echo dying!!" EXIT

echo yeah
echo $UNBOUND_VARIABLE
echo boo

Then, running it gives an exit code 0 even if the script did not really end successfully:

$ bash test.sh
yeah
test.sh: line 8: UNBOUND_VARIABLE: unbound variable
dying!!

$ echo $?
0

But if I comment out the exit trap, the script returns 1. Alternatively, if I replace the line with the unbound variable with a command that returns nonzero (e.g. /bin/false), that exit value is propagated as I would like it to.


Solution

  • This behavior is related to different Bash versions. The original script works as expected on Bash 4.2 but not on 3.2. Having the error-prone code in a separate script file and running it in a subshell works around problems in earlier Bash versions:

    #!/bin/bash
    
    $BASH sub.sh
    RETVAL=$?
    
    if [[ "$RETVAL" != "0" ]]; then
      echo "Dying!! Exit code: $RETVAL"
    fi
    

    sub.sh:

    set -eu
    
    echo yeah
    echo $UNBOUND_VARIABLE
    echo boo