trap-event

EXIT

Run action on shell exit along ordinary and many failure paths. Capture the original status before cleanup and preserve it explicitly. Cannot guarantee cleanup on uncatchable termination.

Formal Syntax & Structure

bash syntaxbash
trap action EXIT

Return Value

event

Run action on shell exit along ordinary and many failure paths. Capture the original status before cleanup and preserve it explicitly. Cannot guarantee cleanup on uncatchable termination.

Working Example

Example Snippetbash
bash -c '    (set -C; : > marker.tmp) || exit 1    cleanup() {        rc=$?        rm -f -- marker.tmp || printf "cleanup failed\n" >&2        printf "cleanup=%s\n" "$rc"        exit "$rc"    }    trap cleanup EXIT    exit 7'rc=$?[[ ! -e marker.tmp ]] && printf 'marker gone\n'printf 'child=%s\n' "$rc"
Bash Reference