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 shell parameter expansion modifier used to assign a default value to a variable if that variable is currently unset or null (empty). Upon evaluation, it mutates the variable’s state by assigning the evaluated fallback string and simultaneously substitutes that newly assigned value into the current expression.
${parameter:=word}

Evaluation Mechanics

When the shell encounters ${parameter:=word}, it evaluates the state of parameter based on the following logic:
  1. Unset: If parameter has not been declared, word is expanded, assigned to parameter, and substituted.
  2. Null: If parameter is declared but contains an empty string (parameter=""), word is expanded, assigned to parameter, and substituted.
  3. Set and Non-Null: If parameter contains a value, word is ignored. No assignment occurs, and the existing value of parameter is substituted.

Syntax Variations and Strictness

The presence or absence of the colon (:) dictates how the shell handles null values:
  • ${parameter:=word} (With colon): Triggers assignment if the variable is unset or null.
  • ${parameter=word} (Without colon): Triggers assignment only if the variable is unset. If the variable is null (an empty string), it retains the empty string, and word is ignored.

Technical Constraints

  • Read-Only Parameters: The := operator cannot be used with positional parameters (e.g., ${1:=default}) or special shell parameters (e.g., ${@:=default}, ${?:=default}). Attempting to do so results in a bad substitution or cannot assign in this way execution error, as these parameters are immutable.
  • Side Effects: Because := performs an in-place assignment, it is an expression with side effects. If the expansion is evaluated as a standalone command, the shell will attempt to execute the resulting string.
To evaluate the assignment purely for its side effect without executing the result, it is conventionally passed as an argument to the : (null) built-in utility:
: ${parameter:=word}
Master Bash with Deep Grasping Methodology!Learn More