The *compound assignment operators* (also called *combination assignment operators*) provide a more concise way of creating [[expression?]]s by enabling calculations involving a variable to be performed without the variable being included in the right hand [[operand?]].
$value += 3; # $value = $value + 3 $value /= 3; # $value = $value / 3
+= | [[addition?]] |
!-= | [[subtraction?]] |
!*= | multiplication |
!/= | division |
%= | modulus |
Care should be taken when compound assignment operators are used within an [[expression?]]. Without parentheses, the compound assignment operators may have a lower precedence than the other operators around them, causing unexpected results to occur:
a = b + c += 2 ; # Syntax error: you cannot assign to 'b + c'. This is equivalent to a = (b + c) += 2 a = b + (c += 2) ; # Ok, fixed. This expression increments c by 2 then adds it to b
The awk extraction and reporting language does not provide support for logical compound assignment operators:
# This will not work because there are # no logical compound assignment operators in awk myflag ||= yourflag myflag &&= bobsflag