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 that substitutes an alternate string value if, and only if, the specified parameter is both set and not null. It acts as a conditional expansion mechanism, evaluating the state of a variable without altering its actual stored value.

Syntax

${parameter:+word}
  • parameter: The name of the variable being evaluated.
  • word: The alternate string or expression to be substituted.

Evaluation Logic

The Bash expansion engine evaluates the state of parameter and yields the following results:
  1. Unset: If parameter has not been declared in the environment, the expression expands to a null string (nothing is substituted).
  2. Null: If parameter is declared but empty (e.g., parameter=""), the expression expands to a null string.
  3. Set and Not Null: If parameter contains any string value of length greater than zero, the expression expands to the evaluated result of word.

Technical Characteristics

  • Non-Destructive: The operation is strictly an expansion. The underlying value of parameter is never modified or overwritten by this operator.
  • Dynamic Evaluation of word: The word token is not treated as a strictly literal string. If the condition is met, word is dynamically parsed and subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion before the final substitution occurs.
  • The Colon Modifier (:): The presence of the colon dictates the strictness of the state test.
    • ${parameter:+word} tests if the parameter is unset or null.
    • ${parameter+word} (omitting the colon) tests only if the parameter is unset. If parameter is set to a null string, ${parameter+word} will successfully expand to word, whereas ${parameter:+word} will not.
Master Bash with Deep Grasping Methodology!Learn More