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"
}
2
  • Forward function declarations in a Bash or a Shell script? Commented Jul 11, 2021 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. Commented Jul 13, 2021 at 18:24

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.