Arithmetic Calculations in Shell

It is used to perform arithmetic operations or Calculations .

Syntax:
expr operator1 mathmetic-operator operator2

Given below is examples:
$ expr 4 + 6
$ expr 6 - 2
$ expr 20 / 4
$ expr 10 % 4
$ expr 30 * 2
$ echo `expr 5 + 8`

Note:
expr 10 % 4 - Reads it as 10 mod 4 and will return the remainder 2.
expr 30 * 2 - Here multiplication uses * and not * since it is used as wild card in shell programming.

For given last statement above note the given following points :

(1) Before expr word we used ` (back quote) sign not the (single quote i.e. ') sign, in given statement it is also ending with ` i.e. back quote.

(2) Here if we use double quotes or single quotes, it will not be able to calculte example :

$ echo "expr 6 + 3" # It will print expr 6 + 3
$ echo 'expr 6 + 3' # It will print expr 6 + 3


0 Comments:

Post a Comment