Join our Newsletter — 33% off our NHI Course
Home› Glossary› Cyber Security› set -e
Cyber Security

set -e

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

A Bash option that attempts to stop a script when a command returns a non-zero status. Its behaviour is narrower than many developers expect because it can skip failures in pipelines, conditionals, subshells, and grouped commands. In practice, it should be treated as one layer of error handling, not a complete safety mechanism.

What set -e actually changes in Bash

The set -e option changes Bash’s default handling of command failures by asking the shell to exit after a non-zero status, but only in specific contexts. It is best understood as a control-flow hint, not a universal error boundary, because Bash does not treat every failed command the same way.

That distinction matters because scripts often combine simple commands, pipelines, conditionals and subshells. In those mixed cases, set -e can appear to provide fail-fast behaviour while still allowing some failures to pass through without terminating the script.

Where set -e behaves differently than many scripts assume

The practical problem is not that set -e is broken, but that its rules are context-sensitive. A command in a conditional test, a pipeline segment, a subshell or a grouped command may not trigger exit in the way a developer expects from reading the option name alone.

That makes the option easy to overestimate. A script can continue after a real failure if the failing command is part of a construct Bash treats specially, so the presence of set -e does not by itself prove that error handling is complete.

For that reason, shell authors usually treat it as one layer in a broader safety pattern, alongside explicit status checks, careful pipeline handling and defensive quoting. The option can reduce some classes of silent failure, but it does not replace deliberate control flow.

Why set -e is often used in shell hardening

People reach for set -e because shell scripts are often glue code, and a single missed failure can cascade into bad file writes, partial deployments or bad cleanup logic. In that sense, the option is about reducing accidental continuation after obvious command failure.

Its value is highest in short, linear scripts where each step depends on the last. In more complex scripts, the same behaviour can become harder to reason about unless the author understands exactly which command forms Bash exempts from immediate exit.

How to think about set -e as part of script reliability

The safest mental model is that set -e narrows the set of failures you may accidentally ignore, but it does not eliminate the need to inspect return codes. That is especially true when commands are intentionally allowed to fail, such as probes, optional lookups or cleanup steps.

Because the behaviour depends on syntax as much as status, the most reliable scripts make failure handling explicit where it matters most. That is also why many security-minded shell guides pair strict mode discussions with pipeline discipline, quoting, and review tools such as ShellCheck.

Risk and Threat Considerations

set -e creates a false sense of safety when a script appears to abort on error but still continues through pipelines, conditionals or subshells. That can turn a failed validation, missing dependency or partial command chain into silent downstream misbehaviour, especially in deployment, backup or privilege-sensitive scripts.

Failure mechanism: Bash does not apply set -e uniformly across all syntactic contexts, so a non-zero exit may be suppressed by the language rules rather than treated as fatal.

Impact: The script can proceed with bad state, incomplete output or unverified assumptions, which increases the chance of corrupted automation and hard-to-diagnose operational failures.

Standards & Framework Alignment

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

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

FrameworkControl / ReferenceRelevance
CIS Controls v8CIS-14 — Security Awareness and Skills TrainingShell scripting errors are a common operator mistake that training should reduce.
Recommendation — Train script authors to understand Bash failure handling and avoid assuming strict mode is sufficient.
NIST SP 800-53 Rev 5SI-2 — Flaw RemediationScript failure handling is part of preventing and correcting operational flaws in automation.
Recommendation — Review and correct shell automation flaws that allow failed commands to continue.
ISO/IEC 27001:2022A.8.28 — Secure codingShell scripts are code and should be developed with secure coding practices that include explicit error handling.
Recommendation — Apply secure coding practices to shell scripts so failures are handled explicitly.
OWASP SAMMImplementation — ImplementationBash script quality improves when secure implementation practices are built into development.
Recommendation — Build explicit error handling into script implementation and review practices.

Practitioner Guidance

Why practitioners should care: Use set -e only as part of a deliberate error-handling strategy, not as proof that every command failure will stop execution. The key judgement is where a failure must be fatal versus where a non-zero exit is expected and should be handled explicitly.

Common misunderstanding: Developers often assume “strict mode” makes Bash scripts uniformly safe. In reality, the surrounding syntax determines whether Bash exits, so the script still needs explicit checks for the commands and paths that matter most.

Practitioner takeaway: Treat set -e as a convenience for reducing noise, then design the script so the important failures are checked and handled on purpose.

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