0

I'm trying to create a calculator that makes basic math operations on shell script but it keeps returning me this syntax error (standard_in) 1: syntax error when I try to multiply two numbers, I tried to find some resolution but nothing helped me until now.

Heres is my code:

echo "====== Calculator ======"
echo "  "

# It will save both numbers to the variables
echo "Type a number: "
read numA
echo "Type another number: "
read numB
echo "  "

# It will give a list of possible operations
echo "Choose an option"
echo "------------------"
echo "1)Addition"
echo "------------------"
echo "2)Subtraction"
echo "------------------"
echo "3)Multiplication"
echo "------------------"
echo "4)Division"
echo "------------------"
echo "  "
read -s opt

# It will make the math behind each operation
case $opt in
        1)result=`echo $numA + $numb | bc`;;
        2)result=`echo $numA - $numB | bc`;;
        3)result=`echo $numA * $numB | bc`;;
        4)result=`echo "scale=2; $numA / $numB" | bc`;;
esac
echo "Result: $result"
  • 1
    `*` is a filename wildcard, and will expand to a list of files in the current directory. You should also double-quote the variables, and... well, pretty much everything. I'd also recommend using `$( )` instead of backticks. – Gordon Davisson Jan 28 '22 at 01:41
  • Does this answer your question? [When should I wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-should-i-wrap-quotes-around-a-shell-variable) (The same advice applies for strings as for variables.) – wjandrea Jan 28 '22 at 02:54
  • BTW, welcome to Stack Overflow! Please take the [tour] and read [ask]. For debugging help in the future, please make a [mre] including minimal code, example input, and expected output. – wjandrea Jan 28 '22 at 02:59
  • Also BTW, you might want to use the [`select` command](https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-select). It's much easier than writing your own menus. – wjandrea Jan 28 '22 at 03:01

1 Answers1

1

Put a back slash before the "*", i.e.

3)result=`echo $numA \* $numB | bc`;;
puravidaso
  • 1,013
  • 1
  • 5
  • 22
  • Thank you so much! Can you send me a link or explain why that back slash was needed? – Lucas Ludicsa Jan 28 '22 at 02:53
  • @LucasLudicsa Because the shell does [filename wildcard expansion](https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html) on arguments before passing them to a command. Compare the output of `echo 5 * 3` vs `echo 5 \* 3`. – Gordon Davisson Jan 28 '22 at 04:21