Simple calculator with menu input - Need Help

I am trying to make a calculator. The user Enters number 1, chooses and operation, enters number 2, then chooses another operation or for the answer to be displayed.

eg. 1 + 1 = or 1 + 1 + 2 + 1 =

Both of these should be possible.

#!/bin/bash

read -p "what's the first number? " n1
PS3="what's the operation? "
select ans in add subtract multiply divide equals; do
case $ans in 
add) op='+' ; break ;;
subtract) op='-' ; break ;;
multiply) op='*' ; break ;;
divide) op='/' ; break ;;
*) echo "invalid response" ;;
esac
done
read -p "what's the second number? " n2
ans=$(echo "$n1 $op $n2" | bc -l)
printf "%s %s %s = %s\n\n" "$n1" "$op" "$n2" "$ans"

exit 0

This is what I have written so far, but i cannot work out how to make it possible to let the user choose 'equals' or to loop back round to enter another operation. Any ideas what I can do to my code here? I have been stuck on this all day.

I dont want the user to enter the equation themselves, i want them to be choosing from a list.

Not sure what operation 'equals' would do, but try:

#!/bin/bash
n1=ans
while [ -n "$n1" ]
do
   read -p "what's the first number? (return to exit)" n1
   [[ -n "$n1" ]] && {
      PS3="what's the operation? "
      select ans in add subtract multiply divide exp; do
         case $ans in
            add) op='+' ; break ;;
            subtract) op='-' ; break ;;
            multiply) op='*' ; break ;;
            divide) op='/' ; break ;;
            exp) op='^' ; break ;;
            *) echo "invalid response" ;;
         esac
      done
      read -p "what's the second number? " n2
      ans=$(echo "$n1 $op $n2" | bc -l)
      printf "%s %s %s = %s\n\n" "$n1" "$op" "$n2" "$ans"
   }
done
 
exit 0

Thank you for the responce, however, i do not have a problem with looping the code. My big issue, is getting the code to work like an actual calculator.

Basically I would like the script to
1 - Prompt the user for a number.
2 - Prompt the user for the operation they would like to perform, either add, subtract, multiply or divide.
3 - Prompt the user for a number.
4 - Prompt the user for the operation they would like to perform, either add, subtract, multiply, divide OR EQUALS.
5 - Display the result of the selected operation. The script should then exit or continue from step 3 if the user has not selected equals.

It is the (continue from step 3) bit that I just cannot get to work. Any ideas. Any help is very welcome, i have been racking my brains with this for too long now :slight_smile:

What would the 'equals' do? It doesn't do the operations immediately, just saves them for later?