Skip to main content

Command Palette

Search for a command to run...

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

Published
2 min read
expr, echo and bc -l supporting arithmetic operations in shell script
M

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.

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

💡
That’s a wrap for today’s post! I hope this has given you some valuable insights. Be sure to explore more articles on our blog for further tips and advice. See you in the next post!

More from this blog

Mihir's Tech blog

69 posts