> ## 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 Display Error if Null or Unset

The `:?` operator is a parameter expansion modifier in Bash used to assert that a variable is set and not null. If the specified parameter is unset or null, Bash evaluates a provided string, writes it to standard error, and immediately aborts the execution of a non-interactive shell with a non-zero exit status. If the parameter is valid, the expression expands to the parameter's value.

```bash theme={"dark"}
${parameter:?word}
```

## Evaluation Mechanics

The behavior of the expansion depends strictly on the state of `parameter`:

* **Set and Not Null:** The expression evaluates to the value of `parameter`. The `word` is ignored.
* **Unset or Null:** Bash expands `word` and writes the result to standard error.
* **Omitted Word:** If `word` is not provided (e.g., `${parameter:?}`), Bash supplies the default diagnostic message strictly as: `parameter null or not set`.

## Error Output Format

When the error is triggered, Bash prefixes the output message depending on the execution environment:

* **Non-interactive shells (Scripts):** The prefix includes the script's name (`$0`) and the line number where the error occurred. Format: `$0: line N: parameter: word` (e.g., `./script.sh: line 3: parameter: word`).
* **Interactive shells (Terminal):** The prefix strictly uses the shell name. Format: `bash: parameter: word`.

## Execution Context Behavior

The operator interacts directly with the shell's execution state:

* **Non-interactive shells (Scripts):** Triggering the error causes the shell to immediately terminate execution with an exit status of `1`. Subsequent commands are not executed.
* **Interactive shells (Terminal):** Triggering the error prints the message to standard error but does *not* exit the shell session.

## Safe Evaluation via the Null Command

Because the expression expands to the variable's value when valid, using it as a standalone command will cause Bash to attempt to execute the expanded value, typically resulting in a `command not found` error. To evaluate the expansion strictly for its assertion side-effect without executing the resulting string, it must be passed as an argument to the shell's null command (`:`). The null command evaluates its arguments and performs expansions, but takes no action and always returns a zero exit status.

```bash theme={"dark"}
: "${parameter:?word}"
```

## Strict Distinction: `:?` vs `?`

Bash differentiates between the presence and absence of the colon (`:`) in the operator, which alters the evaluation of null (empty) strings:

```bash theme={"dark"}

# Fails if 'var' is UNSET or NULL (empty string)
: "${var:?Error Message}"


# Fails ONLY if 'var' is UNSET. An empty string ("") is considered valid and will not trigger the error.
: "${var?Error Message}"
```

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