Shell Programming Blog Shell Programming Blog

Monday 1 October 2012

Perform Arithmetic operations or mathematical calculations in Shell scripting - expr command

Unknown | 09:54 |


 Bash Linux Unix shell scripting

 The command which is used to perform mathematical or Arithmetic calculations is "expr" command.

Syntax:
expr <operand 1> operator <operand 2>



Examples:
$ expr 2 + 3
$ expr 5 - 3
$ expr 7 % 3
$ expr 12 \* 3
$ expr 12 / 6

However, even though the operations looks easy for performing, the "expr" command has a drawback. It uses special character (*) for multiplication which is recognized as a wild character by Linux/Unix. So, if you try to do multiplication as

$ expr 12 * 3

Then you will receive an error. You need to use an escape character (\) along with the (*) character to indicate that this should not be considered as a wild character. Hence the expression should be

$ expr 12 \* 3

If you are going to display the result or assign the result to some variable, then you need to include the expr command in a back tick(`). This symbol is not commonly used. You can find this symbol along with the (~) key in the US keyboard. This is not a single quote('). This is different from single quote and the double quote. Let's see the examples below illustrating the usage.



The back tick is used to inform the Linux system that whatever included within the back tick should be considered as a command and not a string.

To overcome this complexity, Bash shell introduced a new method of doing arithmetic calculations by using brackets. But still the Bash shell includes the expr command to stay compatible with Bourne shell.

Using Brackets:
Another easy way of doing Arithmetic calculation is to enclose the calculation using a dollar sign($) and square brackets ([]).

Examples:



When you are using brackets for doing arithmetic calculations, you don't need to worry about the wild character (*). Linux will consider whatever inside the bracket as  an expression and not as a wild character.

Finally, there is a major limitation in performing math in Bash shell. That is the Bash supports only  integer numbers and not floating numbers. For example if you do the below calculation, you will get only the integer as a result.

$ echo $[100 / 45]
2

But, the z shell (zsh) provides full floating point arithmetic operations. There is also an alternative way to do the floating point calculations in Bash shell. That is by using BC (Bash Calculator).

0 Comments:

   

Post a Comment

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
3. Please do not spam, spam comments will be deleted immediately upon my review.

Regards,
Mohamed Abubakar Sittik A

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming