Contents / Previous / Next


Arithmetics

You can use shell scripts like a calculator: echo "I will work out X*Y" echo "Enter X" read X echo "Enter Y" read Y echo "X*Y = $X*$Y = $[X*Y]" [ and ] mean that everything between must be evaluated.
$[] notation is an extension of the GNU shells and
is not a standard feature on all varients of UNIX.

The standard command to do mathematics
in shell scripts is expr :

X=`expr 100 + 50 '*' 3` echo $X

bc: To do calculations that expr can't handle
Scripts can use bc. For example, convert to decimal
or to binary or work out a SIN:

echo -e 'ibase=16;FFFF' | bc echo -e 'obase=2;12345' | bc pi=`echo "scale=10; 4*a(1)" | bc -l` echo "scale=10; s(45*$pi/180)" | bc -l