There are two ways we can have the shell evaluate an expression. Either using $((expression)) syntax or the expr command.

The former does not require proper spacing, while the latter does.

$ echo $((2+3))
5
$ expr 2 + 3
5

Arithmetic expasion only supports integer values and no decimals.

$ expr 2.3 + 2
expr: not a decimal number: '2.3'

Operators

Here are a list of arithmetic operators.

++ --

Increment, Decrement

+ -

Unary plus and minus

* / %

Multiplication, division and remainder

== !=

Equal and not equal

! && ||

Logical negation, AND, and OR

<< >>

Bit-shift left and right

~ & ^ |

Bitwise negation, AND, exclusive OR, and OR

\= += -= *= /= %=

Assignment operators

&= ^- <<= >>= |=

Bitwise assignment operators

Arithmetic test

It's important to avoid using the = symbol when comparing numerals. The = looks for string equality, not numeric equality. Thus, when working with arithmetic equations, be sure to use -eq instead.

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.