Open-Source Bash Programming • Foundational • Estimated study time: 45 minutes.
An infrastructure diagnostic script may create temporary reports before collecting a result. If a command fails or the operator interrupts the script, those reports should not silently accumulate. Bash traps provide a controlled cleanup path. This lesson extends pipeline error handling and safe expansion.
Learning objectives
Create a private temporary workspace; register an EXIT trap; preserve exit status; handle INT and TERM; distinguish recoverable termination from signals that cannot be trapped; test cleanup without touching production files.
Tutorial: using Bash trap to clean up temporary files when a script exits.
Traps and exit status
The GNU Bash manual documents trap. EXIT runs when the shell exits normally or through a handled exit path. INT and TERM may be trapped, while SIGKILL and SIGSTOP cannot. The conventional status for signal-driven termination is 128 + s, where s is the signal number: INT is 130, TERM is 143. Here the script explicitly chooses those statuses. Capture $? before other commands change it. The GNU mktemp reference explains secure temporary creation; a predictable filename is not an equivalent substitute.

Worked example: a temporary diagnostic report
The cleanup function uses function scope and exit codes and parameter expansion and quoting. Save as osbash010.sh. It creates its own directory and deletes only that path. Do not adapt the deletion command to accept an untrusted path or run it with elevated privileges.
#!/usr/bin/env bash
set -u
work=$(mktemp -d) || exit 1
cleanup() {
local status=$?
trap - EXIT
rm -rf -- "$work" || { printf 'cleanup failed\n' >&2; (( status != 0 )) || status=1; }
exit "$status"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
printf 'workspace=%s\n' "$work"
printf '%s\n' 'synthetic reading: 25 C' > "$work/report" || exit 1
case ${1:-ok} in
ok) cat -- "$work/report" || exit 1 ;;
fail) exit 7 ;;
term) kill -TERM "$$" ;;
int) kill -INT "$$" ;;
*) exit 2 ;;
esac
Local Bash 5.2.37: syntax check passed. Five execution tests confirmed the exit status and that the printed temporary directory no longer existed. Results: ok = 0; fail = 7; term = 143; int = 130; invalid = 2. Tests used synthetic data and local subprocesses.
Community discussion of EXIT cleanup hooks. The GNU manual remains authoritative.
Operational limits
The trap cannot guarantee cleanup after power failure, SIGKILL or all shell crashes. A signal while Bash waits on an external process can also be delayed. Production services need retention policies, independent recovery and bounded temporary storage. Never delete arbitrary directories. The example does not run against real infrastructure. Its cleanup reports failure and preserves an existing nonzero exit status.
Exercises and answers
A. Why call mktemp before installing the trap? B. Why save $? immediately? C. What status does TERM produce here? D. Can SIGKILL trigger the trap? E. What is the local pass rate? Answers: A. Establish a valid private path first. B. Later commands change it. C. 143. D. No. E. Five of five, or 100% for these cases only.
Cover: historical IBM 3151 terminal photograph, Siddharth Patil / Wikimedia Commons, CC0; screen shows FreeBSD rather than the Bash example. Cropped to 1200 × 630.
Editor’s Note: We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please scan the QR code below

For mobile scanning, open the article on another display and scan the QR code with a Bitcoin wallet. The voluntary donation amount is 5,000 sats. Verify the receiving address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb before sending.
BitcoinVersus.Tech publishes technology and financial journalism for informational purposes and does not provide financial, investment, or legal advice.

Leave a Reply