One thing that often confuses new users to the Unix / Linux
shell, is how to do (even very simple) maths. In most languages,
x = x + 1 (or even x++) does exactly what you would expect. The
Unix shell is different, however. It doesn’t have any built-in
mathematical operators for variables. It can do comparisons, but
maths isn’t supported, not even simple addition. Following the
Unix tradition (“do one thing, and do it well”) to the extreme,
because the bc utilites can do maths, there is absolutely no
need for sh to re-invent the wheel. Yes, I agree. This is
frustrating. If I’ve got one gripe against shell programming,
then this is it. Addition and Subtraction So how do we cope?
There are basically two ways, depending on whether we choose
#!/bin/sh echo "Give me a number: " read x echo "Give me another
number: " read y ###### Here's where we have the two options: #
The expr method: exprans=`expr $x + $y` # The bc method:
bcans=`echo $x + $y | bc` ###### Did you see the difference?
echo "According to expr, $x +