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 8978943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:43:41+00:00 2026-06-15T19:43:41+00:00

I’m relatively new to bash programming and i am currently creating a simple calculator.

  • 0

I’m relatively new to bash programming and i am currently creating a simple calculator.

It needs to use floating point numbers and check they are so.

I have a checkNumbers function:

function checkNumber {

    regExp=[0-9]

    if [ $testNo =~ $regExp ]
    then
        echo "That is a number!"
        let check=1
    else
        echo "Damn! Not A Number!"
    fi
}

where i get the user to input a number like this:

while [ $check -eq 0]
do
    echo "Please enter a number
    read testNo
    echo ""
    checkNumber
done

This does not work, i get this error:

./calculator.sh: line 39: [: =~: binary operator expected

line 39 is:

if [ $testNo =~ $regExp ] 

I have tried lots of Regular Expressions like:

^*[0-9]*$

and

^*[0-9]\.[0.9]*$

etc etc.

also, i have tied other ways of performing the check:

case $testNo
in
    ^*[0-9]*$) echo "That is a number!";;
    *) echo "Error! Not a number!";;
esac

and

if [ $testNo = ^*[0-9]*$ ]
then
    echo "etc etc"
else
    echo "oops"
fi

I also need this to work with floating point numbers.

could someone please let me know how i go about this?

  • 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-15T19:43:42+00:00Added an answer on June 15, 2026 at 7:43 pm

    This regex ^[-+]?[0-9]+\.?[0-9]*$ will match only digits with an optional .:

    $ echo 30 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
    Match
    
    $ echo 30.10 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
    Match
    
    $ echo 30. | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
    Match
    
    $ echo +30 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
    Match
    
    $ echo -30 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
    Match
    

    I think when you tried ^*[0-9] you wanted ^[0-9]*

    Rexeplanation:

    ^       # Match start of string
    [-+]?   # Match a leading + or - (optional)
    [0-9]+  # Match one or more digit
    \.?     # Match a literal . (optional, escaped)
    [0-9]*  # Match zero or more digits
    $       # Match the end of the string
    

    Note: this matches numbers followed by a . like 30., not sure if this is acceptable for you.

    Edit: Don’t quote the regex

    testNo=30.00
    
    if [[ $testNo =~ ^[+-]?[0-9]+\.?[0-9]*$ ]]; then 
        echo Match
    fi
    
    >>> Match
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
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 trying to understand how to use SyndicationItem to display feed which is
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 have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
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.