Search code examples
linuxbashshellbc

(standard_in) 1: syntax error in my bash script - bc error


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"

Solution

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

    3)result=`echo $numA \* $numB | bc`;;