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`;;


First number please
Second mumber please




echo $num1+$num2;;

# results in: 1+2    


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").


6 Answers 6


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

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

You can assign from that; also note the $ operators on the variable names inside (()) are optional):


There is also expr:

expr $num1 + $num2

In scripting $(()) is preferable since it avoids a fork/execute for the expr command. In addition, expr’s exit status can be confusing: it’s 0 if the result is not empty or 0, 1 if the result is empty or 0 (this is less confusing if one considers that 0 is conventionally a "normal" exit status and != 0 an error or unusual state).

  • 2
    Or even directly echo $(($num1$op$num2)) without involving case.
    – manatwork
    Commented Sep 30, 2013 at 16:44
  • There's no case involved here.
    – goldilocks
    Commented Sep 30, 2013 at 16:49
  • 2
    No, but you used literal +, so will need the case outside to handle the subtraction separately.
    – manatwork
    Commented Sep 30, 2013 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
    Commented Sep 30, 2013 at 17:04
  • 3
    Note that you can write var instead of $var (the $ prefix is not needed) inside the expression: echo $(( num1 + num2 ))
    – Philipp
    Commented Jun 1, 2021 at 15:26

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

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). Commented Dec 20, 2016 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
    Commented Mar 17, 2017 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
    Commented Mar 17, 2017 at 23:22
  • @evilsoup, now it works like a charm. Thanks.
    – Sigur
    Commented Mar 17, 2017 at 23:30
  • computedval=$(echo 'scale=10;var1-var2' | bc) doesn't work, nor does computedval=$(echo 'scale=10;$var1-$var2' | bc) Commented May 3, 2019 at 1:50

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.

echo $[num1 + num2]
  • Is it safe to assume this isn't POSIX compliant, but Bash specific?
    – flickerfly
    Commented Feb 17, 2021 at 20:14
  • 1
    The $[ expr ] syntax is deprecated and should not be used anymore. More details here: stackoverflow.com/q/2415724/313501
    – Philipp
    Commented Jun 1, 2021 at 15:24
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
    Commented Nov 1, 2015 at 18:07



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.

       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.
q  # This is also typed.

Or, non-interactively:

$ dc -e '1 2 + p'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .