How to Test If a Variable Is a Number in Bash

How do I test if a variable is a number in Bash?

One approach is to use a regular expression, like so:

re='^[0-9]+$'
if ! [[ $yournumber =~ $re ]] ; then
echo "error: Not a number" >&2; exit 1
fi

If the value is not necessarily an integer, consider amending the regex appropriately; for instance:

^[0-9]+([.][0-9]+)?$

...or, to handle numbers with a sign:

^[+-]?[0-9]+([.][0-9]+)?$

How to test if variable is a number with certain max digits in linux non-bash shell script

Not sure if you need this in a case statement, since I would just do:

if { test "$string" -ge 0 && test "$string" -lt 10000; } 2> /dev/null; then
echo good
else
echo bad
fi

If you want to use a case statement in a strictly portable shell, you're probably stuck with:

case $string in 
[0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]) echo good;;
*) echo bad;;
esac

Test whether string is a valid integer

[[ $var =~ ^-?[0-9]+$ ]]
  • The ^ indicates the beginning of the input pattern
  • The - is a literal "-"
  • The ? means "0 or 1 of the preceding (-)"
  • The + means "1 or more of the preceding ([0-9])"
  • The $ indicates the end of the input pattern

So the regex matches an optional - (for the case of negative numbers), followed by one or more decimal digits.

References:

  • http://www.tldp.org/LDP/abs/html/bashver3.html#REGEXMATCHREF

How to check if a number is within a range in shell

If you are using Bash, you are better off using the arithmetic expression, ((...)) for readability and flexibility:

if ((number >= 2 && number <= 5)); then
# your code
fi

To read in a loop until a valid number is entered:

#!/bin/bash

while :; do
read -p "Enter a number between 2 and 5: " number
[[ $number =~ ^[0-9]+$ ]] || { echo "Enter a valid number"; continue; }
if ((number >= 2 && number <= 5)); then
echo "valid number"
break
else
echo "number out of range, try again"
fi
done

((number >= 2 && number <= 5)) can also be written as ((2 <= number <= 5)).


See also:

  • Test whether string is a valid integer
  • How to use double or single brackets, parentheses, curly braces

bash -- how to judge if a variable is a string or number

You could expand the proposed regular expression, dependent on the desired number format(s):

[[ $value =~ ^[0-9]+(\.[0-9]+)?$ ]] would recognize 2 or 2.4 as a number but 2. or .4 as a string.

[[ $value =~ ^(\.[0-9]+|[0-9]+(\.[0-9]*)?)$ ]] would recognize all 2, 2.4, 2. and .4 as numbers

Check whether input is number or not in bash

-o is the or operator. So your test check if your number is not equal to 0 or if it's equals to 0.

(so it should always return true).

To check if it's a number, you could use a regexp: this should be working:

[[ "$number" =~ ^[0-9]+$ ]]

To see the list of all available flags, you should look at man test


Details:

  • [[ is a extended bash test command. It supports more operator than test/[.
  • =~ compare the first argument again a regular expression
  • ^[0-9]+$ is the regular expression. ^ is an anchor for the start of the string, $ for the end. [0-9] means any char between 0 and 9, and + is for the repetition of the latest pattern


Related Topics



Leave a reply



Submit