BASH Script – Arithmetic Manipulations

Three options are used for arithmatic manipulations
1) expr
2) bc
3) (( ))

Example

#Program for Arithmetic Operations
#!/bin/bash
#Author : Antony
#Date : 05/10/2007
 
echo "Enter the first Number "
read x
echo "Enter the second Number "
read y
#Method 1
add=<span style="font-weight: bold; font-style: italic;">$(( $x + $y ))</span>
echo "The Addition is $add"
 
#Method 2
sub=<span style="font-weight: bold; font-style: italic;">`expr $x - $y`</span>
echo "The Subtraction is $sub"
 
#Method 3
div=<span style="font-weight: bold; font-style: italic;">`echo "scale=4; $x / $y" | bc`</span>
echo "The Divison is $div"

Output of this program

VIT:/home/antony# sh arith.sh
Enter the first Number
123
Enter the second Number
124
The Addition is 247
The Subtraction is -1
The Divison is .9919
VIT:/home/antony#

Leave a Response