0

I'm trying to construct a basic calculator, but using functions. I'm getting the error line 17: multiplication: command not found over and over again. So I guess I'm calling the functions wrong? What's the correct way of calling the function and passing parameters then?

#!/bin/bash

echo "Enter the operation:"
read operation

echo "Operand 1:"
read operand_1

echo "Operand 2:"
read operand_2

if [[ "$operation" == "+" ]]
  then 
    addition 
elif [[ "$operation" == "-" ]]
  then 
    subtraction 
elif [[ "$operation" == "*" ]]
  then 
    multiplication 
elif [[ "$operation" == "/" ]]
  then
    division 
fi

addition()
{
  result = $((operand_1+operand_2))
  result $result
}

subtraction()
{
  result = $((operand_1-operand_2))
  result $result

}
multiplication()
{
  result = $((operand_1*operand_2))
  result $result
}

division()
{
  result = $((operand_1/operand_2))
  result $result
}

result()
{
  echo "The result is $1"
}
  • [Forward function declarations in a Bash or a Shell script?](https://stackoverflow.com/q/13588457/995714) – phuclv Jul 11 '21 at 11:17
  • Unrelated but something you'll probably encounter next: assignments don't allow spaces before or after `=`, and function arguments are referred to by number: it has to be one word: `result=$(($1 + $2))` for addition, for example. – chepner Jul 13 '21 at 18:24

1 Answers1

0

You need to define the functions before they are being called in the bash script

abhishek phukan
  • 751
  • 1
  • 5
  • 16