Arithmetic self-operators

Arithmetic self-operators Arithmetic self-operators

These operators work only on numeric operands.
The syntax is:
<target> += <right_operand>
<target> -= <right_operand>
<target> *= <right_operand>
<target> /= <right_operand>
<target> %= <right_operand>
<target> must be an existing variable and contain a numeric value. <right_operand> must evaluate to a numeric value. Note that if you want <right_operand> to be a result of an expression, you must enclose it in the $(*) expression evaluation call.
Operator += sums the <right_operand> value to the <target> value and stores the result in <target>.
Operator -= subtracts <right_operand> from <target> and stores the result in <target>.
Operator *= multiplies <target> by <right_operand> and stores the result in <target>.
Operator /= divides <target> by <right_operand> and stores the result in <target>.
Operator %= computes <target> modulus <right_operand> and stores the result in <target>.
The division and modulus operators fail with an error if <right_operand> is 0.
If both <target> and <right_operand> are integer values then the results of the division and modulus are integers (truncated for the division).
If <target> or <right_operand> or both are floating point values then the result is a floating point value.

Examples

%a=10
echo %a
%a+=20
echo %a
%a-=$(%a - 1)
echo %a
%a *= 10
echo %a
%a /= 21
echo %a
%a *= 20
echo %a
%a /= 21.0
echo %a
%b = 10.0
%a %= %b
echo %a
%a = 10
%b = 3
# nice trick
%a /= %b.0
echo %a

See also
Operators

Index, Language Overview