Mathematical calculations in shellscript

Hi

want to do below mathematical calculations in shellscrip but it is not giving me the exact output.Please help me to solve this

price=95.3
price1=(20/100)*$price+$price
echo "price=$price1"

finally the output should display price=114.36

Thanks

Shell itself doesn't do floating point arithmetics. Use bc or awk:

price=95.3 
price1=$(echo "(20/100)*$price+$price" | bc -l)
$ price1=`echo "scale=2; (20 / 100 * $price) + $price" | bc -l`
$ echo $price1
114.36

---------------------------------
Late post. Already replied by mirni.

In awk ..

$ echo "" | nawk -v price=95.3 '{print (20/100)*price+price}'
114.36

Thanks to all :slight_smile:

Hi.

Suppose we have on file z1:

float price price1
price=95.3
price1="(20./100.)*$price+$price"
echo "price=$price1"

then zsh z1 produces:

% zsh z1
price=1.143600000e+02

and ksh z1 produces:

% ksh z1
price=114.36

For this context:

OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
zsh 4.3.6
ksh 93s+

See man zsh and man ksh (long man pages). Best wishes ... cheers, drl