shell script to check if input is a string/integer/floatBash script, cannote replace string in a file with escaped $ and &Evaluating a string in shell scriptShell script file (.sh) does not run, and throws an errorBash always evaluate Regex as trueexit code of diffreplace a string by variable in a file using bash scriptAutomated Shell script to run fdisk command with user inputCreate bash script that allows you to choose multiple options instead of just one?How to check if a package is installed from Bash?

Horizontal alignment of matrix in an array by using llap and phantom

If password expiration is applied, should door-lock expiration be applied too?

Does Estonia have discount supermarket chains like Aldi and Lidl?

Setting tack strip in concrete

C - wrapping globals in a struct?

Is it necessary to wipe out vile man-eating dragons?

What is the largest piece of space debris volumetrically?

In a world where Magic steam Engines exist what would keep people from making cars

'Cannis' - Term used in seventeenth-century clothes manufacture

Python - Strange interaction with for loop and variable

Can Vice President Pence be impeached before President Trump?

Why is SpaceX not also working on a smaller version of Starship?

How do I negotiate salary when returning to a position I just left?

Can a Rogue exploit a tiny familiar for automatic Sneak Attack in melee?

Inverse Look-and-Say

Why does the Eurofighter Typhoon pitch up on brake release?

Pointing the index fingers to one another as a way to excuse oneself: is this a common gesture?

My advisor wants me to make my PhD thesis weaker

How to evaluate math equation, one per line in a file?

Why 401k contribution as % of salary vs. fixed amount per pay check?

Is Chika Ofili's method for checking divisibility for 7 a "new discovery" in math?

Bought a book that is in the public domain ... but the T&A of company says I can't redistribute it

How was the Luftwaffe able to destroy nearly 4000 Soviet aircraft in 3 days of operation Barbarossa?

Large products with glass doors



shell script to check if input is a string/integer/float


Bash script, cannote replace string in a file with escaped $ and &Evaluating a string in shell scriptShell script file (.sh) does not run, and throws an errorBash always evaluate Regex as trueexit code of diffreplace a string by variable in a file using bash scriptAutomated Shell script to run fdisk command with user inputCreate bash script that allows you to choose multiple options instead of just one?How to check if a package is installed from Bash?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









3


















#!/bin/bash

read -p "Enter value:" val

echo "$val"|grep "^[0-9]*$"
val="$?"

if [[ $val == 0 ]]
then
echo "Integer"
exit
fi

echo $val|grep "^[a-zA-Z]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "String"
exit
fi


echo $val|grep "^[0-9]*.[0-9]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "Float"
exit
fi


If I enter a string like "ape" it says "grep invalid range" and then prints float. Where did I go wrong?










share|improve this question




















  • 1





    What locale are you using? Please edit your question and include the output of locale.

    – terdon
    Sep 14 at 13:43






  • 1





    +1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

    – steeldriver
    Sep 14 at 13:45


















3


















#!/bin/bash

read -p "Enter value:" val

echo "$val"|grep "^[0-9]*$"
val="$?"

if [[ $val == 0 ]]
then
echo "Integer"
exit
fi

echo $val|grep "^[a-zA-Z]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "String"
exit
fi


echo $val|grep "^[0-9]*.[0-9]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "Float"
exit
fi


If I enter a string like "ape" it says "grep invalid range" and then prints float. Where did I go wrong?










share|improve this question




















  • 1





    What locale are you using? Please edit your question and include the output of locale.

    – terdon
    Sep 14 at 13:43






  • 1





    +1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

    – steeldriver
    Sep 14 at 13:45














3













3









3








#!/bin/bash

read -p "Enter value:" val

echo "$val"|grep "^[0-9]*$"
val="$?"

if [[ $val == 0 ]]
then
echo "Integer"
exit
fi

echo $val|grep "^[a-zA-Z]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "String"
exit
fi


echo $val|grep "^[0-9]*.[0-9]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "Float"
exit
fi


If I enter a string like "ape" it says "grep invalid range" and then prints float. Where did I go wrong?










share|improve this question














#!/bin/bash

read -p "Enter value:" val

echo "$val"|grep "^[0-9]*$"
val="$?"

if [[ $val == 0 ]]
then
echo "Integer"
exit
fi

echo $val|grep "^[a-zA-Z]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "String"
exit
fi


echo $val|grep "^[0-9]*.[0-9]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "Float"
exit
fi


If I enter a string like "ape" it says "grep invalid range" and then prints float. Where did I go wrong?







bash scripts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 14 at 13:33









miamia

334 bronze badges




334 bronze badges










  • 1





    What locale are you using? Please edit your question and include the output of locale.

    – terdon
    Sep 14 at 13:43






  • 1





    +1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

    – steeldriver
    Sep 14 at 13:45













  • 1





    What locale are you using? Please edit your question and include the output of locale.

    – terdon
    Sep 14 at 13:43






  • 1





    +1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

    – steeldriver
    Sep 14 at 13:45








1




1





What locale are you using? Please edit your question and include the output of locale.

– terdon
Sep 14 at 13:43





What locale are you using? Please edit your question and include the output of locale.

– terdon
Sep 14 at 13:43




1




1





+1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

– steeldriver
Sep 14 at 13:45






+1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

– steeldriver
Sep 14 at 13:45











2 Answers
2






active

oldest

votes


















2



















This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



#!/bin/bash
read -p "Type a number or a string: " input
if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
echo "Input is an integer."

elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
echo "Input is a string."

elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
echo "Input is a float."

else
echo "Input is a string."
fi





share|improve this answer




























  • "00001" is a string not an integer :+

    – Rinzwind
    Sep 14 at 13:53











  • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

    – terdon
    Sep 14 at 14:18











  • I fixed the + and - problem, but it still chokes on 1..

    – terdon
    Sep 14 at 14:20











  • I edited it so it doesn't choke on 1. anymore.

    – karel
    Sep 14 at 15:05












  • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

    – Hannu
    Sep 14 at 15:46



















3



















I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



LC_ALL=C yourscript.sh


Of course, that won't fix the other problem which is:



echo $val|grep "^[a-zA-Z]*$"

val="$?"


After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




In any case, this is really needlessly complex. All you really need is:



#!/bin/bash

val="$@"

[[ -z $val ]] && echo "No input!" && exit

if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
echo "Number!"
elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
echo "Float!"
elif [[ $val =~ [0-9] ]]; then
echo "Mixed, some numbers"
else
echo "No numbers!"
fi


Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



Also note that I changed some of your terms. I now consider 4 possibilities:



  1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

  2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

  3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

  4. The input has no numbers: print "No numbers".

I split 3 and 4, but you can join them and have them print the same, if you like.



Also, kudos to Karel for thinking of +N and -N numbers.






share|improve this answer





























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1174142%2fshell-script-to-check-if-input-is-a-string-integer-float%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown


























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2



















    This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



    #!/bin/bash
    read -p "Type a number or a string: " input
    if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
    echo "Input is an integer."

    elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
    echo "Input is a string."

    elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
    echo "Input is a float."

    else
    echo "Input is a string."
    fi





    share|improve this answer




























    • "00001" is a string not an integer :+

      – Rinzwind
      Sep 14 at 13:53











    • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

      – terdon
      Sep 14 at 14:18











    • I fixed the + and - problem, but it still chokes on 1..

      – terdon
      Sep 14 at 14:20











    • I edited it so it doesn't choke on 1. anymore.

      – karel
      Sep 14 at 15:05












    • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

      – Hannu
      Sep 14 at 15:46
















    2



















    This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



    #!/bin/bash
    read -p "Type a number or a string: " input
    if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
    echo "Input is an integer."

    elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
    echo "Input is a string."

    elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
    echo "Input is a float."

    else
    echo "Input is a string."
    fi





    share|improve this answer




























    • "00001" is a string not an integer :+

      – Rinzwind
      Sep 14 at 13:53











    • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

      – terdon
      Sep 14 at 14:18











    • I fixed the + and - problem, but it still chokes on 1..

      – terdon
      Sep 14 at 14:20











    • I edited it so it doesn't choke on 1. anymore.

      – karel
      Sep 14 at 15:05












    • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

      – Hannu
      Sep 14 at 15:46














    2















    2











    2









    This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



    #!/bin/bash
    read -p "Type a number or a string: " input
    if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
    echo "Input is an integer."

    elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
    echo "Input is a string."

    elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
    echo "Input is a float."

    else
    echo "Input is a string."
    fi





    share|improve this answer
















    This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



    #!/bin/bash
    read -p "Type a number or a string: " input
    if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
    echo "Input is an integer."

    elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
    echo "Input is a string."

    elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
    echo "Input is a float."

    else
    echo "Input is a string."
    fi






    share|improve this answer















    share|improve this answer




    share|improve this answer








    edited Sep 14 at 15:07

























    answered Sep 14 at 13:50









    karelkarel

    70.8k15 gold badges159 silver badges184 bronze badges




    70.8k15 gold badges159 silver badges184 bronze badges















    • "00001" is a string not an integer :+

      – Rinzwind
      Sep 14 at 13:53











    • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

      – terdon
      Sep 14 at 14:18











    • I fixed the + and - problem, but it still chokes on 1..

      – terdon
      Sep 14 at 14:20











    • I edited it so it doesn't choke on 1. anymore.

      – karel
      Sep 14 at 15:05












    • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

      – Hannu
      Sep 14 at 15:46


















    • "00001" is a string not an integer :+

      – Rinzwind
      Sep 14 at 13:53











    • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

      – terdon
      Sep 14 at 14:18











    • I fixed the + and - problem, but it still chokes on 1..

      – terdon
      Sep 14 at 14:20











    • I edited it so it doesn't choke on 1. anymore.

      – karel
      Sep 14 at 15:05












    • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

      – Hannu
      Sep 14 at 15:46

















    "00001" is a string not an integer :+

    – Rinzwind
    Sep 14 at 13:53





    "00001" is a string not an integer :+

    – Rinzwind
    Sep 14 at 13:53













    This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

    – terdon
    Sep 14 at 14:18





    This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

    – terdon
    Sep 14 at 14:18













    I fixed the + and - problem, but it still chokes on 1..

    – terdon
    Sep 14 at 14:20





    I fixed the + and - problem, but it still chokes on 1..

    – terdon
    Sep 14 at 14:20













    I edited it so it doesn't choke on 1. anymore.

    – karel
    Sep 14 at 15:05






    I edited it so it doesn't choke on 1. anymore.

    – karel
    Sep 14 at 15:05














    Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

    – Hannu
    Sep 14 at 15:46






    Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

    – Hannu
    Sep 14 at 15:46














    3



















    I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



    LC_ALL=C yourscript.sh


    Of course, that won't fix the other problem which is:



    echo $val|grep "^[a-zA-Z]*$"

    val="$?"


    After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




    In any case, this is really needlessly complex. All you really need is:



    #!/bin/bash

    val="$@"

    [[ -z $val ]] && echo "No input!" && exit

    if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
    echo "Number!"
    elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
    echo "Float!"
    elif [[ $val =~ [0-9] ]]; then
    echo "Mixed, some numbers"
    else
    echo "No numbers!"
    fi


    Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



    Also note that I changed some of your terms. I now consider 4 possibilities:



    1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

    2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

    3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

    4. The input has no numbers: print "No numbers".

    I split 3 and 4, but you can join them and have them print the same, if you like.



    Also, kudos to Karel for thinking of +N and -N numbers.






    share|improve this answer
































      3



















      I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



      LC_ALL=C yourscript.sh


      Of course, that won't fix the other problem which is:



      echo $val|grep "^[a-zA-Z]*$"

      val="$?"


      After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




      In any case, this is really needlessly complex. All you really need is:



      #!/bin/bash

      val="$@"

      [[ -z $val ]] && echo "No input!" && exit

      if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
      echo "Number!"
      elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
      echo "Float!"
      elif [[ $val =~ [0-9] ]]; then
      echo "Mixed, some numbers"
      else
      echo "No numbers!"
      fi


      Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



      Also note that I changed some of your terms. I now consider 4 possibilities:



      1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

      2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

      3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

      4. The input has no numbers: print "No numbers".

      I split 3 and 4, but you can join them and have them print the same, if you like.



      Also, kudos to Karel for thinking of +N and -N numbers.






      share|improve this answer






























        3















        3











        3









        I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



        LC_ALL=C yourscript.sh


        Of course, that won't fix the other problem which is:



        echo $val|grep "^[a-zA-Z]*$"

        val="$?"


        After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




        In any case, this is really needlessly complex. All you really need is:



        #!/bin/bash

        val="$@"

        [[ -z $val ]] && echo "No input!" && exit

        if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
        echo "Number!"
        elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
        echo "Float!"
        elif [[ $val =~ [0-9] ]]; then
        echo "Mixed, some numbers"
        else
        echo "No numbers!"
        fi


        Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



        Also note that I changed some of your terms. I now consider 4 possibilities:



        1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

        2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

        3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

        4. The input has no numbers: print "No numbers".

        I split 3 and 4, but you can join them and have them print the same, if you like.



        Also, kudos to Karel for thinking of +N and -N numbers.






        share|improve this answer
















        I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



        LC_ALL=C yourscript.sh


        Of course, that won't fix the other problem which is:



        echo $val|grep "^[a-zA-Z]*$"

        val="$?"


        After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




        In any case, this is really needlessly complex. All you really need is:



        #!/bin/bash

        val="$@"

        [[ -z $val ]] && echo "No input!" && exit

        if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
        echo "Number!"
        elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
        echo "Float!"
        elif [[ $val =~ [0-9] ]]; then
        echo "Mixed, some numbers"
        else
        echo "No numbers!"
        fi


        Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



        Also note that I changed some of your terms. I now consider 4 possibilities:



        1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

        2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

        3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

        4. The input has no numbers: print "No numbers".

        I split 3 and 4, but you can join them and have them print the same, if you like.



        Also, kudos to Karel for thinking of +N and -N numbers.







        share|improve this answer















        share|improve this answer




        share|improve this answer








        edited Sep 14 at 14:22

























        answered Sep 14 at 14:11









        terdonterdon

        75.9k14 gold badges153 silver badges239 bronze badges




        75.9k14 gold badges153 silver badges239 bronze badges































            draft saved

            draft discarded















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1174142%2fshell-script-to-check-if-input-is-a-string-integer-float%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown









            Popular posts from this blog

            Tamil (spriik) Luke uk diar | Nawigatjuun

            Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

            Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?