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

  • SEARCH
  • Home
  • 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 8887215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:45:06+00:00 2026-06-14T21:45:06+00:00

I’m writing a simple calculator in BASH. Its aim is to loop through the

  • 0

I’m writing a simple calculator in BASH. Its aim is to loop through the arguments given, check if they’re correct, do the power function and pass the rest to the expr to do the calculation. Everything except multiplication works.
When I do something like

my_script.sh 2 \* 2  

I get syntax error from expr. Checking with bash -x lets me know that

expr 2 '\*' 2  

The * is in apostrophes. I don’t know how to get rid of it so the expr can parse it properly.

if [ $# -le 0 ]
then
    usage
    exit 1
fi

ARGS=("${@}")

while [ $# -gt 0 ]
do
    if [ $OP -eq 0 ]
    then
        if [[ $1 =~ ^[-]{0,1}[0-9]+$ ]]
        then
            ELEMS[$J]=$1
            shift
            let OP=1
            let J=$J+1
        else
            echo $1' is not a number'
            usage
            exit 3
        fi
    else
        if [[ $1 =~ ^[-+/\^\*]{1}$ ]]
        then
            if [[ $1 =~ ^[\^]{1}$ ]]
            then
                if ! [[ $2 =~ ^[0-9]+$ ]]
                then
                    echo 'Bad power exponent'
                    usage
                    exit 3
                fi
                let BASE=${ELEMS[$J-1]}
                let EX=$2
                pow $BASE $EX
                let ELEMS[$J-1]=$RES
                shift 2
            else
                if [[ $1 =~ [\*]{1} ]]
                then
                    ELEMS[$J]=\\*
                else
                ELEMS[$J]=$1
                fi
                let J=$J+1
                shift
                let OP=0
            fi
        else
            echo $1' is not an operator'
            if [[ $1 =~ ^[0-9]+$ ]]
            then
                let TMP=${ELEMS[$J-1]}
                echo "Are you missing an operator beetwen $TMP and $1?"
            fi
            usage
            exit 3
        fi
    fi    
done

if [ $OP -eq 0 ]
then
    echo 'Missing argument after last operator'
    usage
    exit 3
fi
echo "Calculation: ${ARGS[*]}"
echo -n 'Result: '
expr ${ELEMS[*]}
  • 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-14T21:45:07+00:00Added an answer on June 14, 2026 at 9:45 pm

    Change ELEMS[$J]=\\* to ELEMS[$J]="*" (or ELEMS[$J]=\*) and use:

    expr "${ELEMS[@]}"
    

    The key is to use @ instead of * in the array dereference, which allows you to use double quotes. This is equivalent to expr "2" "*" "2", instead of expr "2 * 2" which you get when using expr "${ELEMS[*]}"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker

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.