0

Probably a simple question but I am a beginner
I am using the terminal on Mac
I want to add up numbers entered by the user and print the result on screen

#!/bin/sh
echo “please enter the first no”;read a
echo “please enter the second no”; read b
c=$((a+b))
echo “the answer is $c”

It asks for the inputs but returns "?? back instead of adding up the numbers
Thanks

Luna
  • 5
  • 1
  • 2
    Change to `“` to `"` – eyllanesc Nov 23 '17 at 02:44
  • 1
    I see no problem with script, i tried it works.. whats your output ? – DarkKnight Nov 23 '17 at 02:44
  • 2
    If you put a space between `$c` and the `”`, it works. I'm not quite sure how shell is treating the quote (U+201D, RIGHT DOUBLE QUOTATION MARK), but it manages not to expand `$c` properly (as wanted). The moral of the story is "do not use a word processor to edit shell code (or C code, or …)". Use a U+0022 QUOTATION MARK and not the word processing U+201C (LEFT DOUBLE QUOTATION MARK) and U+201D characters to surround strings. Or configure your editor not to map the `"` key to the other symbols. – Jonathan Leffler Nov 23 '17 at 03:30
  • Possible duplicate of [MacOSX: how to disable accented characters input](https://stackoverflow.com/questions/12723236/macosx-how-to-disable-accented-characters-input) – Makyen Nov 24 '17 at 06:08

2 Answers2

1

The code looks OK, but it could be simplified:

read -p "Please enter two numbers: " a b && echo The sum is $((a+b))

Sample run (where the user enters 333 and 33333):

Please enter two numbers: 333 33333
The sum is 33666
agc
  • 7,973
  • 2
  • 29
  • 50
0

This should work

#!/bin/sh
echo “please enter the first no”;read a
echo “please enter the second no”; read b
c=$(( a + b ))
echo "the answer is $c"

the quotes you have around line 5 need to be " and ", not “ and ”.

Chris Hill
  • 235
  • 1
  • 8