Bash Arithmetic


Unary Operators

Unary operators have the highest priority.

Operator

Meaning

a++

post-increment $a

a--

post-decrement $a

++a

pre-increment $a

--a

pre-decrement $a

+

positive

-

negative

!

negation


Binary Operators

The binary operators include arithmetic and logic operations.

Operator

Meaning

*

multiplication

/

division

%

remainder

+

addition

-

subtraction

**

exponentiation

<

less than

>

greater than

<=

less than or equal

>=

greater than or equal

==

equality

!=

inequality


Ternary Operators

There is just one ternary operator:

a ? b : c

a is the test, b is the value if true, and c is the value if false.

Note that ternary operators follow logic operators as well.


Assignment

Operator

Meaning

a = b

assignment

a *= b

a = a * b

a /= b

a = a / b

a %= b

a = a % b

a += b

a = a + b

a -= b

a = a - b

Note that ternary operators follow logic operators as well.


Bit-wise Operators

Operator

Meaning

~

negation

<<

bitshift left

>>

bitshift right

&

AND

^

XOR

|

OR

a <<= b

a = a << b

a >>= b

a = a >> b

a &= b

a = a & b

a ^= b

a = a ^ b

a |= b

a = a | b

Note that negation has equal precedence to other unary operators; bitshifts (such as <<) follow arithmetic operators; bit-wise logic (such as &) come after all other binary operators; and bit-wise assignment operators (such as <<=) have equal precedence to other assignment operators.


CategoryRicottone