linux - [Solved-5 Solutions] How to test if a variable is a number in Bash - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

Code:

test *isnumber* $1 && VAR=$1 || echo "need a number"
click below button to copy the code. By - Linux tutorial - team

How to test if a variable is a number in Bash ?

Linux - Solution 1:

To use a regular expression, like this:

re='^[0-9]+$'
if ! [[ $yournumber =~ $re ]] ; then
   echo "error: Not a number" >&2; exit 1
fi
click below button to copy the code. By - Linux tutorial - team

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

^[0-9]+([.][0-9]+)?$
click below button to copy the code. By - Linux tutorial - team

To handle negative numbers:

^-?[0-9]+([.][0-9]+)?$
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 2:

case $string in
    ''|*[!0-9]*) echo bad ;;
    *) echo good ;;
esac
click below button to copy the code. By - Linux tutorial - team

This rejects empty strings and strings containing non-digits, accepting everything else.

Negative or floating-point numbers need some additional work. An idea is to exclude - / . in the first "bad" pattern and add more "bad" patterns containing the inappropriate uses of them (?*-* / *.*.*)

Linux - Solution 3:

"$var" -eq "$var"
click below button to copy the code. By - Linux tutorial - team

as in:

#!/bin/bash

var=a

if [ "$var" -eq "$var" ] 2>/dev/null; then
  echo number
else
  echo not a number
fi
click below button to copy the code. By - Linux tutorial - team

Redirection of standard error is there to hide the "integer expression expected" message that bash prints out in case we do not have a number.

  • Numbers with decimal points are not identified as valid "numbers"
  • Using [[ ]] instead of [ ] will always evaluate to true
  • Most non-Bash shells will always evaluate this expression as true
  • The behavior in Bash is undocumented and may therefore change without warning
  • If value includes spaces after number (e.g. "1 a") produces error, like bash: [[: 1 a: syntax error in expression (error token is "a")
  • If value is the same as var-name (e.g. i="i"), produces error, like bash: [[: i: expression recursion level exceeded (error token is "i")

Linux - Solution 4:

Solutions directly parsing number formats in shell. shell is not well suited to this, being a DSL for controlling files and processes. There are ample number parsers a little lower down, for example:

isdecimal() {
  # filter octal/hex/ord()
  num=$(printf '%s' "$1" | sed "s/^0*\([1-9]\)/\1/; s/'/^/")

  test "$num" && printf '%f' "$num" >/dev/null 2>&1
}
click below button to copy the code. By - Linux tutorial - team

Change '%f' to whatever particular format you require.

Linux - Solution 5:

This tests if a number is a non negative integer and is both shell independent (i.e. without bashisms) and uses only shell built-ins:

[ -z "${num##[0-9]*}" ] && echo "is a number" || echo "is not a number";
click below button to copy the code. By - Linux tutorial - team
[ ! -z "${num##*[!0-9]*}" ] && echo "is a number" || echo "is not a number";
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - How to test if a variable is a number in Bash