Linux 如何在Shell脚本中添加用户输入的数字

Linux 如何在Shell脚本中添加用户输入的数字,linux,shell,Linux,Shell,可能是个简单的问题,但我是初学者 我正在Mac上使用终端 我想把用户输入的数字加起来,然后在屏幕上打印结果 #!/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” 它请求输入,但返回“?”而不是将数字相加 谢谢这应该行得通 #!/bin/sh echo “please enter the first

可能是个简单的问题,但我是初学者
我正在Mac上使用终端
我想把用户输入的数字加起来,然后在屏幕上打印结果

#!/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”
它请求输入,但返回“?”而不是将数字相加
谢谢这应该行得通

#!/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"

第5行的引号必须是“and”,而不是“and”。

代码看起来不错,但可以简化:

read -p "Please enter two numbers: " a b && echo The sum is $((a+b))
示例运行(用户输入333和33333):


更改为
我发现脚本没有问题,我尝试了它。您的输出是什么?如果在
$c
之间加一个空格,它就可以工作了。我不太确定shell是如何处理这个报价的(U+201D,右双引号),但它设法不正确地扩展
$c
(如需要)。这个故事的寓意是“不要使用文字处理器编辑shell代码(或C代码,或…)。使用U+0022引号而不是字处理U+201C(左双引号)和U+201D字符来包围字符串。或者将编辑器配置为不将
键映射到其他符号。是的可能重复问题在于引号
Please enter two numbers: 333 33333
The sum is 33666