> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Bash ERR Trap

The `ERR` trap in Bash is an event-driven exception handling mechanism that executes a specified command or function whenever a command, pipeline, list, or arithmetic evaluation terminates with a non-zero exit status. It intercepts failures synchronously at the point of execution, temporarily suspending the script's normal flow to evaluate the trap payload.

## Syntax

```bash theme={"dark"}

# Execute a string/command on error
trap 'command_to_execute' ERR


# Execute a predefined function on error
trap function_name ERR


# Remove the ERR trap
trap - ERR
```

## Execution Mechanics

When a command yields a non-zero exit status, Bash evaluates the `ERR` trap payload in the current shell environment. Once the trap completes, script execution resumes at the command immediately following the one that failed, unless the trap explicitly invokes `exit` or the shell is operating under `set -e` (`errexit`). If `set -e` is enabled, the `ERR` trap executes immediately *before* the shell terminates.

## Contextual Variables

During the execution of an `ERR` trap, Bash populates specific environment variables that provide context about the failure:

* `$?`: Contains the integer exit status (1-255) of the command that triggered the trap.
* `$BASH_COMMAND`: Contains the literal string of the command that failed, exactly as it was evaluated by the shell.
* `$LINENO`: Contains the script line number where the failing command was executed.

```bash theme={"dark"}
trap 'echo "Command $BASH_COMMAND failed at line $LINENO with status $?"' ERR
```

## Scope and Inheritance (`errtrace`)

By default, `ERR` traps are strictly bound to the execution environment in which they are declared. They are **not** inherited by shell functions, command substitutions, or subshells.

To force the `ERR` trap to propagate down the call stack into sub-environments, you must enable the `errtrace` option:

```bash theme={"dark"}
set -E

# or
set -o errtrace
```

## Trigger Exceptions

The `ERR` trap adheres to the exact same suppression rules as the `set -e` (`errexit`) option. The trap will **not** trigger if the failing command is part of a conditional test or a boolean chain where the failure is considered handled by the shell's logic.

Specifically, the `ERR` trap is suppressed when the failing command is:

1. Part of the condition in an `if` or `elif` statement.
2. Part of the condition in a `while` or `until` loop.
3. Part of a boolean list connected by `&&` or `||`, except for the final command in the chain.
4. Negated using the `!` operator (e.g., `! false`).
5. Any command in a pipeline other than the last command, unless `set -o pipefail` is enabled.

```bash theme={"dark"}
trap 'echo "Error"' ERR


# WILL trigger the trap
ls /nonexistent_directory


# WILL NOT trigger the trap (suppressed by boolean OR)
ls /nonexistent_directory || true


# WILL NOT trigger the trap (suppressed by conditional context)
if ls /nonexistent_directory; then
    echo "Success"
fi
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Bash Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
