Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9160307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:42:28+00:00 2026-06-17T13:42:28+00:00

I am trying to make a calculator with a bash script. The user enters

  • 0

I am trying to make a calculator with a bash script.
The user enters a number, chooses whether they wish to add, subtract, multiply or divide. Then the user enters a second number and is able to choose whether to do the sum, or add, subtract, multiply or divide again on a loop.

I cannot rack my head around this right now

echo Please enter a number
read number

echo What operation would you like to perform: 1: Add, 2: Subtract, 3: Multiple, 4: Divide
read operation

case $operation in
    1) math='+';;
    2) math='-';;
    3) math='*';;
    4) math='/';;
    *) math='not an option, please select again';;
esac
echo "$number $math"


echo Please enter a number
read number2

echo What operation would you like to perform: 1: Add, 2: Subtract, 3: Multiple, 4: Divide, 5: Equals
read operation2
case $operation2 in
    1)math2='Add';;
    2)math2='Subtract';;
    3)math2='Multiply';;
    4)math2='Divide';;
    5)math2='Equals';;
    *)math2='not an option, please select again';;
esac
echo You have selected $math2

exit 0

This is what I have done so far, but can anyone help me work out how to loop back on the calculator?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:42:29+00:00Added an answer on June 17, 2026 at 1:42 pm

    The lesser-known shell builtin command select is handy for this kind of menu-driven program:

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

    Sample output

    what's the first number? 5
    what's the second number? 4
    1) add
    2) subtract
    3) multiply
    4) divide
    what's the operation? /
    invalid response
    what's the operation? 4
    5 / 4 = 1.25000000000000000000
    

    If I was going to get fancy with bash v4 features and DRY:

    #!/bin/bash
    
    PS3="what's the operation? "
    declare -A op=([add]='+' [subtract]='-' [multiply]='*' [divide]='/')
    
    while true; do
        read -p "what's the first number? " n1
        read -p "what's the second number? " n2
        select ans in "${!op[@]}"; do
            for key in "${!op[@]}"; do
                [[ $REPLY == $key ]] && ans=$REPLY
                [[ $ans == $key ]] && break 2
            done
            echo "invalid response"
        done
        formula="$n1 ${op[$ans]} $n2"
        printf "%s = %s\n\n" "$formula" "$(bc -l <<< "$formula")"
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a calculator. The user Enters number 1, chooses and
I'm trying to make a calculator which is basicly like this: user enters about
I am trying to make a simple calculator program where a user can opt
I'm trying to make my first Python calculator which can add given values given
I am trying to make a calculator where a user can select from a
I'm trying to make a simple credit card repayment calculator script but seem to
I'm trying to make a complex calculator and the user has to select from
I'm currently trying to make an orientation calculator in java and I'm having a
I am trying to make a BMI Calculator using HTML. The inputs come from
I'm trying to make a simple addition calculator in C to learn how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.