Skip to main content

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.

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.
${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.
: "${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:

# 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}"
Master Bash with Deep Grasping Methodology!Learn More