Im currently trying to make a addition command in unix and have come up with the following code:

#! /bin/bash
#! Add - adds two given numbers together and displays the result

"$num1" = $1
"$num2" = $2

echo "Enter two numbers"
        read num1 num2
        sum=$(“$num1” + “$num2”)
                echo "The sum is = $sum"

This however does not work.

  • 1
    Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"? – Eric Renouf Nov 25 '15 at 19:38
  • When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is = – S.Jones Nov 25 '15 at 19:46
  • 1
    num1=$1. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them. – chepner Nov 25 '15 at 21:14

((...)) is the way to do arithmetic, not single parens, and you don't need quotes there Try:

| improve this answer | |

Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.

That means that the script could be reduced to


printf 'The sum of %d and %d is %d\n' "$1" "$2" "$(( $1 + $2 ))"

This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.

The script would be used as

$ ./script.sh -23 32
The sum of -23 and 32 is 9
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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