Join our Newsletter — 33% off our NHI Course
Home› Glossary› Cyber Security› Trap
Cyber Security

Trap

← Back to Glossary
By NHI Mgmt Group Updated October 31, 2026 Domain: Cyber Security

A Bash mechanism for running a command when a script receives a signal or encounters an error condition. In defensive scripting, traps are used for cleanup, logging, and tracing execution so operators can see where a failure occurred. They improve observability, but they do not replace explicit validation or error checking.

What a Bash trap is

A Bash trap is a signal or error handler that tells the shell to run a command when a script exits, receives an interrupt, or hits a specified failure condition. It gives scripts a controlled way to react to abnormal execution, especially when cleanup or diagnostics must happen before control is lost.

How traps work in shell execution

Traps are bound to events such as exit, interrupt, terminate, or specific signals, and Bash executes the associated command when that event occurs. This makes trap behavior part of the script’s control flow, not a separate monitoring layer, so the command must be concise, reliable, and safe to run in a partially failed state.

Because traps run during abnormal execution paths, they inherit the context of the script at the moment the event occurs. That is useful for closing files, removing temporary artifacts, or writing a final status message, but it also means any trapped command can fail if the script state is already damaged or resources are unavailable.

Why traps matter for observability

In defensive scripting, traps improve observability by making the point of failure more visible and by preserving evidence of what the script was doing when it stopped. A well-placed trap can log exit status, emit tracing detail, or record cleanup actions that would otherwise be lost when execution aborts unexpectedly.

That visibility is especially useful in automation, where silent failures can leave partial changes behind. Traps help explain where a script stopped, but they do not prove the script handled the error correctly or that its inputs were validated before use.

Trap limitations and safe use

Traps are a reaction mechanism, not a substitute for explicit checks. They cannot replace defensive validation, robust error handling, or careful command sequencing, because they only run after an event has already occurred and may not fire in every failure mode a script can encounter.

They are most effective when used for narrow, predictable tasks such as cleanup on exit, minimal logging, or restoring state. Overloading a trap with complex logic can make failures harder to diagnose and can introduce new bugs in the very path meant to improve resilience.

Risk and Threat Considerations

Traps create a useful control point, but they can also become a reliability and security weak spot if the trapped command is too complex, depends on fragile state, or assumes resources still exist after a failure. In defensive scripts, a broken trap can leave temporary files, partial configuration changes, or incomplete logs behind.

Failure mechanism: The script reaches an abnormal exit path, but the trap itself fails, runs too late, or performs unsafe cleanup against a corrupted environment.

Impact: Operators lose the cleanup or trace they expected, and the script may leave behind artifacts, inconsistent state, or weak forensic evidence that slows recovery and root-cause analysis.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

CIS Controls v8 and NIST SP 800-53 Rev 5 set the technical controls, while ISO/IEC 27001:2022 defines the regulatory obligations.

FrameworkControl / ReferenceRelevance
CIS Controls v8CIS-16 — Application Software SecurityTraps are part of defensive scripting and safe error handling in software operations.
Recommendation — Use secure coding practices to keep error handlers simple, reliable, and testable.
NIST SP 800-53 Rev 5AU-12 — Audit Record GenerationTrap-based logging supports traceability when scripts exit or fail unexpectedly.
SI-11 — Error HandlingTraps are one mechanism for handling and reporting script errors and abnormal conditions.
Recommendation — Generate useful exit and failure logs so scripts preserve evidence during abnormal termination. Handle script errors explicitly and ensure failure paths do not hide or corrupt state.
ISO/IEC 27001:2022A.8.28 — Secure codingTraps are a coding construct whose safe use depends on secure script design and failure handling.
Recommendation — Design shell scripts so cleanup and diagnostics remain safe under failure conditions.

Practitioner Guidance

Why practitioners should care: Use traps for bounded, low-risk actions that still make sense during failure, such as emitting a final status line or removing scratch data. Keep the handler small enough that it can run even when the surrounding script is only partially functional.

Common misunderstanding: A trap does not make a shell script safe by itself, and it does not replace validation, strict exit handling, or explicit checks around each critical command. Treat it as a last-chance hook for cleanup and visibility, not as the primary safety mechanism.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on October 31, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org