81

I can read the numbers and operation in with:

echo "First number please"
read num1
echo "Second number please"
read num2
echo "Operation?"
read op

but then all my attempts to add the numbers fail:

case "$op" in
  "+")
    echo num1+num2;;
  "-")
    echo `num1-num2`;;
esac

Run:

First number please
1
Second mumber please
2
Operation?
+

Output:

num1+num2

...or...

echo $num1+$num2;;

# results in: 1+2    

...or...

echo `$num1`+`$num2`;;

# results in: ...line 9: 1: command not found

Seems like I'm getting strings still perhaps when I try add add ("2+2" instead of "4").

  • 4
    I edited your title because this is a good general question, and if you narrowed the context down you would have realized this has nothing to do with case or anything except one line: echo $num+$num, since that will reproduce the problem exactly. The idea with minimizing context in programming questions is explained here: sscce.org – goldilocks Sep 30 '13 at 16:47
  • 1
    +1 Hi goldilocks, yes that became clear to me afterwards, but yes I totally agree with your line of reasoning and yes, the more specific and 'singular' a question is the better. I welcome your edit(s) :) Thank You :) – Michael Durrant Sep 30 '13 at 17:04
104

Arithmetic in POSIX shells is done with $ and double parentheses:

echo "$(($num1+$num2))"

You can assign from that (sans echo):

num1="$(($num1+$num2))"

There is also expr:

expr $num1 + $num2

In scripting $(()) is preferable since it avoids a fork/execute for the expr command.

  • Or even directly echo $(($num1$op$num2)) without involving case. – manatwork Sep 30 '13 at 16:44
  • There's no case involved here. – goldilocks Sep 30 '13 at 16:49
  • 1
    No, but you used literal +, so will need the case outside to handle the subtraction separately. – manatwork Sep 30 '13 at 16:51
  • 1
    @manatwork : You mean eliminate the case from the OP's code with echo $(($num1$op$num2)) -- that will work, but using the case is more robust since you can handle errors with a default *. – goldilocks Sep 30 '13 at 17:04
  • +1 to both of you. I was using case (might not have shown it at the start) but as you both realized the problem wasn't about the case. – Michael Durrant Sep 30 '13 at 17:05
12

The existing answer is pure bash, so it will be faster than this, but it can only handle integers. If you need to handle floats, you have to use the external program bc.

$ echo 'scale=4;3.1415+9.99' | bc
13.1315

The scale=4 tells bc to use four decimal places. See man bc for more information.

  • Note that while bash doesn't support floating point arithmetics with $((...)), many shells (ksh93, zsh, yash at least) do. The advantage of bc is that it supports arbitrary precision while shell arithmetics is limited to the processor's double type. Note that you don't need to set scale here. For additions, the scale parameter is not used. The scale of 3.1415+9.99 will be derived from that of the operands (here 4). – Stéphane Chazelas Dec 20 '16 at 12:06
  • How to do the math if one value is 450 and the other is decimal stored on a variable $mynumber? For example, 450-$mynumber. – Sigur Mar 17 '17 at 22:34
  • @sigur echo "450-$mynumber" | bc should do it. If you want a variable to expand you have to use double quotes rather than single quotes. – evilsoup Mar 17 '17 at 23:22
  • @evilsoup, now it works like a charm. Thanks. – Sigur Mar 17 '17 at 23:30
  • computedval=$(echo 'scale=10;var1-var2' | bc) doesn't work, nor does computedval=$(echo 'scale=10;$var1-$var2' | bc) – StatsSorceress May 3 at 1:50
4
echo `expr $a + $b`   
echo `expr $a - $b`   
echo `expr $a \* $b`   
echo `expr $a / $b`   

Note the \ before the * (for multiplication), the whole expression has to be within the backquotes `.

  • 1
    expr is not necessary, and it's never necessary to capture the standard output of a command just to write it back to standard output. – chepner Nov 1 '15 at 18:07
2

minimalist

total=0
((total+=qty))
0

Based on the sequence of inputs you request from the user, it seems you are using reverse polish notation.

echo "First number please"
read num1
echo "Second number please"
read num2
echo "Operation?"
read op

You may do better just to use dc (desk calculator) directly, since that is what it is for.

DESCRIPTION
       Dc is a reverse-polish desk calculator which supports unlimited pre-
       cision arithmetic.

Example session using dc:

$ dc
1 2 + p    # This part is typed; the result comes next.
3
q  # This is also typed.
$

Or, non-interactively:

$ dc -e '1 2 + p'
3
$
0

You can also use $[ ... ] structure. In this case, we use built-in mechanizm in Bash, which is faster and a bit more convenient to use. Since we know that everything between $[, and ] is treated as an expression, we don't need to precede the variables with $. Similarily, we do not need to secure * from treating it like a pattern.

num1=2
num2=3
echo $[num1 + num2]
5

protected by Community Dec 20 '16 at 12:03

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.