> ## 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 RETURN Trap

The `RETURN` trap in Bash is an event-driven mechanism that executes a specified command list whenever a shell function or a sourced script terminates. It is triggered either by an explicit `return` statement or by the implicit completion of the execution flow (reaching the end of the function or script block).

## Syntax

```bash theme={"dark"}

# Define a RETURN trap
trap 'command_list' RETURN


# Unset a RETURN trap
trap - RETURN
```

## Execution Mechanics

* **Trigger Timing:** The trap executes immediately after the function or sourced script finishes, but strictly *before* control is handed back to the calling environment.
* **Exit Status (`$?`):** The exit status evaluated immediately prior to the trap's execution is the exit status of the returning function or script. The commands executed within the trap do not alter the final return value of the function unless the trap explicitly executes a new `return` command.
* **Sourced Scripts vs. Subprocesses:** The trap fires when a script is executed via the `source` or `.` builtins. It does *not* trigger when a script is executed as a standard child process (e.g., `./script.sh`), as that constitutes an `EXIT` condition, not a `RETURN`.

## Scope and Inheritance (`functrace`)

By default, the `RETURN` trap (along with the `DEBUG` trap) is not inherited by shell functions, command substitutions, or subshell environments. A `RETURN` trap defined in the global scope will not automatically trigger when a child function returns.

To propagate the `RETURN` trap to these environments, the `functrace` attribute must be enabled using the `set` builtin:

```bash theme={"dark"}

# Enable RETURN/DEBUG trap inheritance
set -T

# Alternatively: set -o functrace


# Disable RETURN/DEBUG trap inheritance
set +T

# Alternatively: set +o functrace
```

When `functrace` is enabled, any invoked function will inherit the `RETURN` trap from its calling environment, causing the trap to fire upon the termination of every function in the execution tree.

## Contextual Variables

When a `RETURN` trap is triggered, Bash preserves the execution context of the returning entity. The following internal arrays remain accessible and reflect the state of the function or script that just returned:

* `FUNCNAME`: Contains the execution call stack. `${FUNCNAME[0]}` holds the name of the function that triggered the trap.
* `BASH_SOURCE`: Contains the source filenames corresponding to the `FUNCNAME` stack.
* `BASH_LINENO`: Contains the line numbers in the source files where each function in the stack was invoked.

<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>
