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

# Binary addition
result=$(( 5 + 3 ))


# Unary plus
result=$(( +5 ))


# Arithmetic evaluation via 'let' builtin
let "result = 5 + 3"


# Arithmetic compound assignment
(( variable += 5 ))

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.

# Evaluates to 'alternate_string' if var is set
echo "${var+alternate_string}"


# Evaluates to 'alternate_string' if var is set AND not empty
result="${var:+alternate_string}"

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.

# Requires enabling extended globbing
shopt -s extglob


# Matches one or more occurrences of pattern1 or pattern2
ls +(pattern1|pattern2)

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.

# Disables a shell option via short flag (e.g., xtrace)
set +x


# Disables a shell option via long name (e.g., errexit)
set +o errexit


# Removes the export attribute from a variable
declare +x var_name


# Removes the integer attribute from a local variable
local +i var_name

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.

# Matches one or more digits
[[ $string =~ [0-9]+ ]]

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.

# Arithmetic addition (integer attribute set)
declare -i int_var=5
int_var+=5  # Evaluates to 10


# String concatenation (default attribute)
string_var="text"
string_var+=" appended" # Evaluates to "text appended"


# Appends elements to an existing indexed array
indexed_array+=(element1 element2)


# Appends elements to an existing associative array
associative_array+=([key1]=element1 [key2]=element2)
Master Bash with Deep Grasping Methodology!Learn More