Receive all updates via Facebook. Just Click the Like Button Below

Arithmetics

Here we will learn how to perform arithmetic operations using shell scripts.

1

Arithmetics in Linux Shell Scripting

2

We use the keyword " expr " to perform arithmetic operations.

Syntax:
expr op1 math-operator op2

Example:

 
$ expr 3 + 2
$ expr 3 - 2
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 3 + 2`

In shell scripting we will be using the echo command to print the result of the arithmetic operations.

Note:
If you use echo command, before expr keyword we use ` (back quote) sign.
Here both double and single quote will not give you the desired result.
Try the following commands:

 
$ echo "expr 3 + 2" # Result expr 3 + 2
$ echo 'expr 3 + 2'  # Result expr 3 + 2
$ echo `expr 3 + 2` # Result 5

From the above example, we see that if we use double or single quote, the echo command directly prints " expr 3 + 2 ".
To get the result of this expression, we need to use the back quote.

3

Related posts:

Advertise here

picture picture

Follow us

picture

- Tech-Tutorials twitter.com/techcuriosity

Like us

About Techcuriosity

I am Telson, the webmaster of Techcuriosity.com and through this site, I share my knowledge and curiosities in the Tech World.


What We Offer

  • What is ..?
  • How to ..?
  • Difference between ..?
  • How .. works?
  • Get in Touch

    techcuriosity@gmail.com
    UK : +44 77 33 890 678

    picture picture

    Our Partners

  • Website Design Dubai
  • SEO Link Exchange
  • picture

    picture