Tutorials Point


  Unix for Beginners
  Unix Shell Programming
  Advanced Unix
  Unix Useful References
  Unix Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Unix - Shell Arithmatic Operators Example


previous next AddThis Social Bookmark Button


Here is an example which uses all the arithmatic operatos:

#!/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 expresssion 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.



previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names