set -eo pipefail trap_exit_fun() { exit_code=$? echo "exit code: $exit_code" if [ "$exit_code" != "0" ]; then echo "error!" fi } trap trap_exit_fun EXIT echo "start" xxxxx # error here echo "end"