Shell Arithmetics: A Beginner's Guide
Learn how to perform basic arithmetic operations in the shell.
Introduction
The shell can be used to perform some basic arithmetic such as addition, subtraction, multiplication, division, modulus operation, exponential etc.
It can also be used to perform some more advanced operations like the logical operation, the bitwise operation, the equality and inequality operation and the comparison operation.
Thus, the shell can also act as a basic calculator.
Arithmetic Operators
The shell accommodates some mathematical operators that are not new, they are also the same operators that are been supported by most programming languages and even our calculators. These operators are:
Name | Operator | Use | ||||
pre-increment & post-increment | ++num & num++ | num = num + 1 | ||||
pre-decrement & post-decrement | --num & num-- | num = num - 1 | ||||
unary plus | + | |||||
unary minus | - | |||||
Logical negation | ! | !num | ||||
Bitwise negation | ~ | ~num | ||||
exponentiation | ** | num**power | ||||
multiplication | * | num1 * num2 | ||||
division | / | num1 / num2 | ||||
remainder | % | num1 % num2 | ||||
addition | + | num1 + num2 | ||||
subtraction | - | num1 - num2 | ||||
left bitwise shift | << | num1 << num2 | ||||
right bitwise shift | \>> | num1 >> num2 | ||||
less than or equal to | <= | num1 <= num2 | ||||
greater than or equal to | \>= | num1 >= num2 | ||||
less than | < | num1 < num2 | ||||
greater than | \> | num1 > num2 | ||||
equality | \== | num1 == num2 | ||||
inequality | != | num1 != num2 | ||||
bitwise AND | & | num1 & num2 | ||||
bitwise Exclusive OR | ^ | num1^num2 | ||||
bitwise OR | num1 | num2 | ||||
logical AND | && | num1 && num2 | ||||
logical OR | num1 | num2 | ||||
Conditional operator | expr ? expr : expr | (3 == 4) ? True:False | ||||
assignments | *\= \= /= %= += -= <<= >>= &= ^= | =** | num1 *= num2 | |||
comma | expr1, expr2 | (num1+num2), (num1*num2) |
This is the most common arithmetic operation one can carry out on the shell.
Arithmetic Expressions
To perform simple arithmetic calculations on the shell, we will have to be able to construct simple arithmetic expressions.
Arithmetic expressions are when we combine an operator and an operand to perform simple calculations on the operand.
Let's say we have two numbers, 8 and 4 which we want to perform an addition, subtraction, multiplication, division and remainder operation, we will have to do that by using each of the operators that are responsible for these operations on the two operands (8 and 4).
So to perform these operations on these numbers on the shell, we are going to have the following:
echo $((8+4))
echo $((8-4))
echo $((8*4))
echo $((8/4))
echo $((8%4))
check the image below to see the result we will be getting for each of these operations:
All these are simple expressions, we can also have complex operations, where we will be having more than one expression at the same time. Let us combine the multiplication expression and divide it by the subtraction operation.
echo $(($((8*4))/$((8-4))))
So this is how one can perform some complex expressions. Remember that each expression should have its dollar sign $
, enclosed inside double brackets, to avoid errors and complications.
Using Variables
We have talked about how that shell supports variables, so these same variables can be used to perform calculations as well.
For example, let us take these two numbers, 10 and 5 and store them inside the variables NUM1 and NUM2. And afterward, perform some basic arithmetic operations on them.
NUM1=10
NUM2=5
echo $(($NUM1+$NUM2))
echo $(($NUM1-$NUM2))
echo $(($NUM1*$NUM2))
echo $(($NUM1/$NUM2))
Here is the result of what you will get for each of them:
This is the basics of Shell Arithmetic.
Conclusion
Other advanced calculations can be done on the shell, using this same concept of shell arithmetic, but for this tutorial, we didn't touch those aspects. These aspects even use functions and some more advanced methods like loops, control statements and others etc.
Thank you for reading. You can connect with me on Twitter and LinkedIn.