Unix - Shell Arithmetic Operators Example

Advertisements


Here is an example which uses all the arithmetic operators:

#!/bin/sh

a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"

val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

if [ $a == $b ]
then
   echo "a is equal to b"
fi

if [ $a != $b ]
then
   echo "a is not equal to b"
fi

This would produce following result:

a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a is not equal to b

There are following points to note down:

  • There must be spaces between operators and expressions for example 2+2 is not correct, where as it should be written as 2 + 2.

  • Complete expression should be enclosed between ``, called inverted commas.

  • You should use \ on the * symbol for multiplication.

  • if...then...fi statement is a decision making statement which has been explained in next chapter.



Advertisements
Advertisements