What trap does
trap ACTION SIGNAL... tells bash to run ACTION when one of the named signals or conditions occurs. ACTION is a string of shell code, and it runs in the shell that set the trap. A trap does not change how the signal is delivered to a child program. It only changes what the shell does in response to the signal or condition in its own process.
The most useful condition is the pseudo-signal EXIT. Bash runs the EXIT trap once, when the shell is about to exit, whether the script finished normally, called exit, or stopped because of a failure in strict mode. That single rule covers almost every case where cleanup matters.
A cleanup that always runs
Set the trap near the top of the script, right after you create the resource. The trap string should be simple, and it should refer to a variable that holds the path being cleaned. Quote the path inside the trap string, so that a path with spaces is removed correctly.
work=/workspace/work.d
mkdir -p "$work"
trap 'rm -rf "$work"; echo "cleaned"' EXIT
echo "working in $work"
exit 2
echo "not reached"The output shows working in /workspace/work.d, then the exit 2 ends the script before the last echo. The trap runs on that exit as well, so the cleaned line is printed and the directory is gone. The quoting of the trap string uses single quotes so that $work is expanded when the trap runs, not when it is set.
Double quotes around the trap string expand variables immediately, when trap is called. Single quotes postpone the expansion until the trap runs. Both are valid, but the choice changes the value you get if the variable changes later.
Preserving the exit status
The exit status of a script is the status of its last command, or the value passed to exit. A cleanup command that runs last can overwrite that value. If the trap runs a command that succeeds, the status of the script becomes zero, even though the script failed. Save the status at the start of the trap and exit with it at the end.
bash -c 'trap "echo bye" EXIT; exit 3'; echo "status=$?"The output is bye followed by status=3. The trap ran, and the exit status of the script was kept. A trap that wants to change the status must end with an explicit exit, and most cleanup traps should not.
Signals and their default meanings
Signals are small messages the kernel delivers to processes. The three you will see most are INT, sent by Ctrl-C to the foreground process group, TERM, sent by kill by default, and HUP, sent when a terminal closes. A script can trap these to clean up before it stops, so that a user who presses Ctrl-C does not leave a mess behind.
bash -c 'trap "echo got TERM; exit 0" TERM; kill -TERM $$; echo "not reached"'The script sends itself TERM, the trap prints got TERM and exits with status zero, and the final echo never runs. The $$ is the process ID of the shell, so the signal goes to the script itself. Only INT, TERM, and HUP are normal targets for cleanup; KILL cannot be trapped, because the kernel stops the process without giving it a chance to act.
Ignoring and resetting traps
An empty action, written as trap '' INT, tells the shell to ignore the signal. A dash with no action, written as trap - INT, restores the default. Use ignore only for short sections where an interruption would leave a half-finished change, and reset the trap afterward.
Hands-on: watch the EXIT trap
Run the block below and compare its output with your expectation. It registers an EXIT trap and then exits with status 4.
bash -c 'trap "echo cleanup ran" EXIT; echo working; exit 4'; echo "status=$?"The output is working, then cleanup ran, then status=4. The lab for this unit asks you to write a script with a cleanup trap and a script that keeps its exit status, and checks both.