Operations in the Arithmetic Expressions
Explore how to perform arithmetic, bitwise, logical, and assignment operations in Bash expressions. Understand the priority of these operations to write clear and accurate scripts. Learn how to use numeral system prefixes and format output with printf for versatile number handling.
We'll cover the following...
We'll cover the following...
The operations in arithmetic expression
The following table shows the operations we can perform in arithmetic expressions.
| Operation | Description | Example |
|---|---|---|
| Calculations | ||
* |
Multiplication | echo "$((2 * 9)) = 18" |
/ |
Division | echo "$((25 / 5)) = 5" |
% |
The remainder of division | echo "$((8 % 3)) = 2" |
+ |
Addition | echo "$((7 + 3)) = 10" |
- |
Subtraction | echo "$((8 - 5)) = 3" |
** |
Exponentiation | echo "$((4**3)) = 64" |
| Bitwise operations | ||
~ |
Bitwise NOT | echo "$((~5)) = -6" |
<< |
Bitwise left shift | echo "$((5 << 1)) = 10" |
>> |
Bitwise right shift | echo "$((5 >> 1)) = 2" |
& |
Bitwise AND | echo "$((5 & 4)) = 4" |
| |
Bitwise OR | echo "$((5 | 2)) = 7" |
^ |
Bitwise XOR | echo "$((5 ^ 4)) = 1" |
| Assignments | ||
= |
Ordinary |