Skip to main content
The + operator in Bash is a context-dependent token whose behavior is dictated by the syntactic construct in which it is evaluated. It functions primarily as an arithmetic addition and unary operator, a parameter expansion modifier, an extended globbing quantifier, a regular expression quantifier, a string or array concatenation operator, and a shell option or variable attribute toggle.

Arithmetic Evaluation

Within arithmetic expansion contexts, + functions as a standard binary addition operator for integer arithmetic. Bash does not natively support floating-point arithmetic; therefore, operands are strictly evaluated as integers. It also functions as a unary plus operator to explicitly denote a positive integer, and it can be combined with the assignment operator (+=) to increment a variable.

Parameter Expansion (Alternate Value)

In parameter expansion, + is used to test whether a variable is set, substituting an alternate string if the condition is met. It does not modify the original variable.
  • ${parameter+word}: If parameter is declared (even if null), the expansion evaluates to word. If parameter is unset, it evaluates to nothing.
  • ${parameter:+word}: The addition of the colon enforces a strict non-null check. If parameter is declared and not null, it evaluates to word.

Extended Globbing (extglob)

When the extglob shell option is enabled, + acts as a pattern quantifier. It matches one or more occurrences of the specified pattern list. The pattern list can contain multiple patterns separated by the pipe (|) character.

Shell Option and Attribute Toggling

When used with the set builtin, the + operator disables shell options, adhering to the POSIX standard specification for shell execution environments. This is the inverse of the - operator, which enables them. This identical syntax applies to variable declaration builtins (declare, local, typeset) to remove specific attributes from a variable.

Regular Expression Quantifier

Within the [[ ]] conditional construct utilizing the =~ binary operator, the right-hand side is evaluated as a POSIX Extended Regular Expression (ERE). In this context, + functions as the standard regex quantifier matching one or more occurrences of the preceding element.

Compound Assignment (+=)

The behavior of the += operator outside of explicit arithmetic contexts is strictly determined by the target variable’s attributes and type:
  • Integer Variables: If the variable has the integer attribute set (e.g., via declare -i), the += operator evaluates the right-hand side as an arithmetic expression and performs arithmetic addition.
  • String Variables: If the integer attribute is not set, the += operator performs literal string concatenation.
  • Arrays: When applied to arrays using compound assignment syntax, += appends elements. Indexed arrays append values sequentially, whereas associative arrays require explicit key-value subscripts.
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More