expr, echo and bc -l supporting arithmetic operations in shell script

Table of contents
- Last Blog Review →
- 1. The expr command
- Syntax of expr command
- Examples of using expr command
- 2. echo with double parathesis →
- Here we don’t need to separate the operands and the operators with space. Also not required to escape the * with backslash.
- Examples of using echo with operators
- 3. bc -l i.e. basic calculator for floating point number
- Conclusion
- Image credit →

Last Blog Review →
In the last blog we understood, how to install package the network way of installation.
1. The expr command
The expr
command evaluates expressions and returns the result. It only supports integer arithmetic.
Syntax of expr command
expr operand1 operator operand2
Examples of using expr command
i-002978a646c30dd8b (EC2)
-------------------------
--37-59:~$ expr 6 + 3
9
--37-59:~$ expr 6 - 3
3
--37-59:~$ expr 6 / 3
2
--37-59:~$ expr 6 \* 3 #Here * is reserver characte meaning "everything". So, to escape it we use backslash.
18
2. echo with double parathesis →
Here we don’t need to separate the operands and the operators with space. Also not required to escape the * with backslash.
Examples of using echo with operators
i-002978a646c30dd8b (EC2)
-------------------------
$ A = 6
$ B = 3
$ echo $(( A + B ))
9
$ echo $(( A-B ))
3
$ echo $((A/B))
2
$ echo $(( A * B ))
18
3. bc -l i.e. basic calculator for floating point number
i-002978a646c30dd8b (EC2)
-------------------------
$ A = 6
$ B = 3
$ echo $A / $B | bc -l
3.33333
Conclusion
In the this blog we understood, the use of echo with double parenthesis, expr, and use of basic calculator in shell scripting.
Image credit →
https://images.app.goo.gl/anz9Pex6eeRkTqf16
Subscribe to my newsletter
Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Mihir Suratwala
Mihir Suratwala
Hi, How are you !! Hope you doing good.... I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective. So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.