04 February 2012

Arithmetic Operators in Shell Script

1 comment

Here are Some Arithmetic Operators Used in Shell Script.

OPERATOR DESCRIPTION
+ Add two Numbers
- Subtract two Numbers
* Multiply two Numbers
/ Division
% Modulus
= Assignment

Example


#!/bin/bash

a=5
echo -e "value of a is $a\n"

echo -e "assigning value of a to b\n"
b=$a
echo -e "value of b is $b\n"

echo -e "adding two number\n"
echo `expr $a + $b`
echo " "

echo -e "subtracting two number\n"
echo `expr $a - $b`
echo " "

echo -e "multiplying two numbers\n"
echo `expr $a \* $b`
echo " "

echo -e "dividing two numbers\n"
echo `expr $a / $b`
echo " "

echo -e "modulus of two number"
echo `expr $a % $b`

Note: There should be no space when using Assignment operator.
Note: * is a wild card , therefore we have to suppress it using \ , other wise it will display error message.

Output



That's it.
Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

1 comment: