> ## 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 Exit Status

An exit status (or return code) is an 8-bit unsigned integer value between `0` and `255` returned by an executed command, script, or shell function to indicate its termination state. External commands and scripts return this value to their parent process, whereas shell functions return it to the caller within the current shell execution environment. In Bash, an exit status of `0` denotes success, while any non-zero value (`1` through `255`) indicates an error or abnormal termination.

## Accessing the Exit Status

Bash stores the exit status of the most recently executed foreground pipeline in the special parameter `$?`.

```bash theme={"dark"}
command
echo $?
```

Because `$?` is overwritten after every command execution, it must be captured immediately into a variable if the value needs to be evaluated multiple times.

```bash theme={"dark"}
command
STATUS=$?
```

## Setting the Exit Status

The exit status of a script or shell session is explicitly defined using the `exit` builtin. For shell functions and sourced scripts (e.g., `source ./script.sh` or `. ./script.sh`), the `return` builtin is used.

```bash theme={"dark"}

# Terminate script with a specific status
exit 1


# Terminate function or sourced script with a specific status
return 0
```

If `exit` or `return` is invoked without an integer argument, or if a script/function terminates without an explicit `exit`/`return` statement, the exit status defaults to the exit status of the last executed command within that scope.

Because the exit status is an 8-bit integer, passing a numeric value outside the `0-255` range results in a modulo 256 operation (e.g., `exit 256` evaluates to an exit status of `0`, and `exit -1` evaluates to `255`).

## Reserved Exit Codes

Bash and POSIX standards reserve specific non-zero exit codes to denote particular failure states.

| Code      | Meaning                   | Technical Trigger                                                                       |
| :-------- | :------------------------ | :-------------------------------------------------------------------------------------- |
| `0`       | Success                   | Command completed its intended operation.                                               |
| `1`       | General Error             | Catchall for general, non-specific failures.                                            |
| `2`       | Misuse of shell builtins  | Syntax error or missing keyword in a builtin command.                                   |
| `126`     | Cannot execute            | Command is found, but lacks executable permissions.                                     |
| `127`     | Command not found         | Command does not exist in `$PATH` or as a builtin/function.                             |
| `128 + N` | Fatal error signal        | Process was terminated by signal `N` (e.g., `130` for SIGINT/Ctrl+C, which is 128 + 2). |
| `255`     | Non-numeric exit argument | `exit` was passed a non-integer argument (e.g., `exit foo`).                            |

## Exit Status in Pipelines

By default, the exit status of a pipeline is the exit status of the last (rightmost) command in the chain.

```bash theme={"dark"}
command1 | command2 | command3

# $? contains the exit status of command3
```

To access the exit statuses of all commands within a pipeline, Bash provides the `PIPESTATUS` array variable. It contains a list of exit status values corresponding to the processes in the most recently executed foreground pipeline.

Like `$?`, the `PIPESTATUS` array is highly volatile and is overwritten by the execution of the very next command. If a developer attempts to evaluate its elements sequentially (e.g., across multiple `if` statements or `echo` commands), the array will be overwritten after the first check. To evaluate multiple values, the entire array must be captured immediately.

```bash theme={"dark"}
command1 | command2 | command3


# Capture the entire array immediately
statuses=("${PIPESTATUS[@]}")


# Elements can now be evaluated safely
echo "Status of command1: ${statuses[0]}"
echo "Status of command2: ${statuses[1]}"
echo "Status of command3: ${statuses[2]}"
```

If the `pipefail` option is enabled via `set -o pipefail`, the pipeline's exit status becomes the value of the last (rightmost) command to exit with a non-zero status. If all commands exit successfully, the pipeline returns `0`.

## Logical Evaluation and Negation

Bash control structures (`if`, `while`, `until`) and logical operators (`&&`, `||`, `!`) evaluate the exit status of commands directly. They do not evaluate boolean strings; they evaluate the integer returned to the shell.

* **`&&` (AND):** Executes the right-hand command only if the left-hand command returns `0`.
* **`||` (OR):** Executes the right-hand command only if the left-hand command returns a non-zero value.
* **`!` (NOT):** Inverts the exit status of a pipeline. If the command returns `0`, `!` changes the exit status to `1`. If the command returns a non-zero value, `!` changes it to `0`.

```bash theme={"dark"}

# Logical AND / OR
command1 && command2
command1 || command2


# Logical NOT
! command

# $? is 1 if command succeeded, 0 if command failed


# Control structure evaluation
if ! command; then
    # Executes if command returns a non-zero status
fi
```

The `true` and `false` builtins exist solely to return specific exit statuses for these evaluations. `true` always returns `0`, and `false` always returns `1`.

## Automatic Termination on Error

To enforce strict error handling based on exit statuses, Bash provides the `errexit` option. Enabled via `set -e` or `set -o errexit`, this setting instructs the shell to terminate immediately if any pipeline, list, or compound command yields a non-zero exit status.

```bash theme={"dark"}
set -e
command_that_fails  # Script terminates immediately here
command_skipped     # This will not execute
```

The `errexit` option does not trigger termination if the failing command is part of a condition in an `if`, `while`, or `until` statement, part of a `&&` or `||` list (except the final command), or if its exit status is explicitly inverted using the `!` operator.

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