I just can't figure out how do I make sure an argument passed to my script is a number or not.
All I want to do is something like this:
test *isnumber* $1 && VAR=$1 || echo "need a number"
Any help?
I just can't figure out how do I make sure an argument passed to my script is a number or not.
All I want to do is something like this:
test *isnumber* $1 && VAR=$1 || echo "need a number"
Any help?
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]+)?$
^-?
rather than ^-*
unless you're actually doing the work to handle multiple inversions correctly.
– Charles Duffy
Jun 26 '11 at 22:57
[[ $yournumber =~ ^[0-9]+$ ]]
.
– konsolebox
Aug 31 '13 at 21:48
Without bashisms (works even in the System V sh),
case $string in
''|*[!0-9]*) echo bad ;;
*) echo good ;;
esac
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 (?*-*
/ *.*.*
)
if test ...
– tripleee
Sep 4 '11 at 13:21
${string#-}
(which doesn't work in antique Bourne shells, but works in any POSIX shell) to accept negative integers.
– Gilles 'SO- stop being evil'
Jan 3 '12 at 17:17
'.' | *.*.*
to the disallowed patterns, and add dot to the allowed characters. Similarly, you can allow an optional sign before, although then I would prefer case ${string#[-+]}
to simply ignore the sign.
– tripleee
Jun 7 '14 at 12:10
The following solution can also be used in basic shells such as Bourne without the need for regular expressions. Basically any numeric value evaluation operations using non-numbers will result in an error which will be implicitly considered as false in shell:
"$var" -eq "$var"
as in:
#!/bin/bash
var=a
if [ -n "$var" ] && [ "$var" -eq "$var" ] 2>/dev/null; then
echo number
else
echo not a number
fi
You can can also test for $? the return code of the operation which is more explicit:
[ -n "$var" ] && [ "$var" -eq "$var" ] 2>/dev/null
if [ $? -ne 0 ]; then
echo $var is not number
fi
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.
CAVEATS (thanks to the comments below):
[[ ]]
instead of [ ]
will always evaluate to true
true
bash: [[: 1 a: syntax error in expression (error token is "a")
bash: [[: i: expression recursion level exceeded (error token is "i")
[[ a -eq a ]]
evaluates to true (both arguments get converted to zero)
– Tgr
Aug 28 '12 at 9:30
if ! [ $# -eq 1 -o "$1" -eq "$1" ] 2>/dev/null; then
– haridsv
Aug 2 '13 at 13:07
[
builtin will evaluate the arguments as arithmetic. That is true in both ksh93 and mksh. Further, since both of those support arrays, there is easy opportunity for code injection. Use a pattern match instead.
– ormaaj
Oct 8 '14 at 5:34
[[ ]]
but not for [ ]
. That said, this behavior is unspecified by both the POSIX standard for test
and in bash's own documentation; future versions of bash could modify behavior to match ksh without breaking any documented behavioral promises, so relying on its current behavior persisting is not guaranteed to be safe.
– Charles Duffy
Mar 26 '16 at 18:43
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:
INCORRECT.
As this first answer (below) allows for integers with characters in them as long as the first are not first in the variable.
[ -z "${num##[0-9]*}" ] && echo "is a number" || echo "is not a number";
CORRECT .
As jilles commented and suggested in his answer this is the correct way to do it using shell-patterns.
[ ! -z "${num##*[!0-9]*}" ] && echo "is a number" || echo "is not a number";
*[!0-9]*
is a pattern that matches all strings with at least 1 non-digit character. ${num##*[!0-9]*}
is a "parameter expansion" where we take the content of the num
variable and remove the longest string that matches the pattern. If the result of the parameter expansion is not empty (! [ -z ${...} ]
) then it's a number since it does not contain any non-digit character.
– mrucci
May 26 '16 at 19:00
Nobody suggested bash's extended pattern matching:
[[ $1 == ?(-)+([0-9]) ]] && echo "$1 is an integer"
shopt -s extglob
from your post (that I upvoted, it's one of my favorite answers here), since in Conditional Constructs you can read: When the ==
and !=
operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below in Pattern Matching, as if the extglob
shell option were enabled. I hope you don't mind!
– gniourf_gniourf
Feb 13 '15 at 19:49
shopt extglob
... that's a good thing to know!
– gniourf_gniourf
Feb 13 '15 at 19:49
3.2.25(1)-release
of bash: -bash: syntax error in conditional expression: unexpected token
(' -bash: syntax error near `?(-'. In that release, the featured noted by @gniourf_gniourf is not working.
– Jdamian
Sep 8 '16 at 7:07
I'm surprised at the 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
}
Change '%f' to whatever particular format you require.
isnumber(){ printf '%f' "$1" &>/dev/null && echo "this is a number" || echo "not a number"; }
– Gilles Quenot
Sep 28 '12 at 18:33
isnumber 23 && echo "this is a number" || echo "not a number"
– michael
Jul 18 '13 at 23:54
2>/dev/null
, so that isnumber "foo"
does not pollute stderr?
– gioele
Jun 6 '14 at 6:10
isnumber "'a"
will return true. This is documented in the POSIX spec where you'll read: If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote.
– gniourf_gniourf
Feb 13 '15 at 19:37
I was looking at the answers and... realized that nobody thought about FLOAT numbers (with dot)!
Using grep is great too.
-E means extended regexp
-q means quiet (doesn't echo)
-qE is the combination of both.
To test directly in the command line:
$ echo "32" | grep -E ^\-?[0-9]?\.?[0-9]+$
# answer is: 32
$ echo "3a2" | grep -E ^\-?[0-9]?\.?[0-9]+$
# answer is empty (false)
$ echo ".5" | grep -E ^\-?[0-9]?\.?[0-9]+$
# answer .5
$ echo "3.2" | grep -E ^\-?[0-9]?\.?[0-9]+$
# answer is 3.2
Using in a bash script:
check=`echo "$1" | grep -E ^\-?[0-9]*\.?[0-9]+$`
if [ "$check" != '' ]; then
# it IS numeric
echo "Yeap!"
else
# it is NOT numeric.
echo "nooop"
fi
To match JUST integers, use this:
# change check line to:
check=`echo "$1" | grep -E ^\-?[0-9]+$`
Just a follow up to @mary. But because I don't have enough rep, couldn't post this as a comment to that post. Anyways, here is what I used:
isnum() { awk -v a="$1" 'BEGIN {print (a == a + 0)}'; }
The function will return "1" if the argument is a number, otherwise will return "0". This works for integers as well as floats. Usage is something like:
n=-2.05e+07
res=`isnum "$n"`
if [ "$res" == "1" ]; then
echo "$n is a number"
else
echo "$n is not a number"
fi
'BEGIN { exit(1-(a==a+0)) }'
is slightly hard to grok but can be used in a function which returns true or false just like [
, grep -q
, etc.
– tripleee
Aug 2 '17 at 7:01
test -z "${i//[0-9]}" && echo digits || echo no no no
${i//[0-9]}
replaces any digit in the value of $i
with an empty string, see man -P 'less +/parameter\/' bash
. -z
checks if resulting string has zero length.
if you also want to exclude the case when $i
is empty, you could use one of these constructions:
test -n "$i" && test -z "${i//[0-9]}" && echo digits || echo not a number
[[ -n "$i" && -z "${i//[0-9]}" ]] && echo digits || echo not a number
man -P 'less +/parameter\/' bash
part. Learning something new every day. :)
– David
Apr 11 at 10:09
\-
in regular expression to address the issue. Use [0-9\-\.\+]
to account for floats and signed numbers.
– user2683246
May 9 at 14:42
echo $i | python -c $'import sys\ntry:\n float(sys.stdin.read().rstrip())\nexcept:\n sys.exit(1)' && echo yes || echo no
– user2683246
May 9 at 15:15
Old question, but I just wanted to tack on my solution. This one doesn't require any strange shell tricks, or rely on something that hasn't been around forever.
if [ -n "$(printf '%s\n' "$var" | sed 's/[0-9]//g')" ]; then
echo 'is not numeric'
else
echo 'is numeric'
fi
Basically it just removes all digits from the input, and if you're left with a non-zero-length string then it wasn't a number.
$'0\n\n\n1\n\n\n2\n\n\n3\n'
.
– gniourf_gniourf
Feb 13 '15 at 21:44
I would try this:
printf "%g" "$var" &> /dev/null
if [[ $? == 0 ]] ; then
echo "$var is a number."
else
echo "$var is not a number."
fi
Note: this recognizes nan and inf as number.
%f
is probably better anyway)
– michael
Jul 19 '13 at 0:01
if
itself? That's what if
does... if printf "%g" "$var" &> /dev/null; then ...
– Camilo Martin
Jun 26 '14 at 11:00
'a
.
– gniourf_gniourf
Feb 13 '15 at 21:30
Can't comment yet so I'll add my own answer, which is an extension to glenn jackman's answer using bash pattern matching.
My original need was to identify numbers and distinguish integers and floats. The function definitions deducted to:
function isInteger() {
[[ ${1} == ?(-)+([0-9]) ]]
}
function isFloat() {
[[ ${1} == ?(-)@(+([0-9]).*([0-9])|*([0-9]).+([0-9]))?(E?(-|+)+([0-9])) ]]
}
I used unit testing (with shUnit2) to validate my patterns worked as intended:
oneTimeSetUp() {
int_values="0 123 -0 -123"
float_values="0.0 0. .0 -0.0 -0. -.0 \
123.456 123. .456 -123.456 -123. -.456
123.456E08 123.E08 .456E08 -123.456E08 -123.E08 -.456E08 \
123.456E+08 123.E+08 .456E+08 -123.456E+08 -123.E+08 -.456E+08 \
123.456E-08 123.E-08 .456E-08 -123.456E-08 -123.E-08 -.456E-08"
}
testIsIntegerIsFloat() {
local value
for value in ${int_values}
do
assertTrue "${value} should be tested as integer" "isInteger ${value}"
assertFalse "${value} should not be tested as float" "isFloat ${value}"
done
for value in ${float_values}
do
assertTrue "${value} should be tested as float" "isFloat ${value}"
assertFalse "${value} should not be tested as integer" "isInteger ${value}"
done
}
Notes: The isFloat pattern can be modified to be more tolerant about decimal point (@(.,)
) and the E symbol (@(Ee)
). My unit tests test only values that are either integer or float, but not any invalid input.
A clear answer has already been given by @charles Dufy and others. A pure bash solution would be using the following :
string="-12,345"
if [[ "$string" =~ ^-?[0-9]+[.,]?[0-9]*$ ]]
then
echo $string is a number
else
echo $string is not a number
fi
Although for real numbers it is not mandatory to have a number before the radix point.
To provide a more thorough support of floating numbers and scientific notation (many programs in C/Fortran or else will export float this way), a useful addition to this line would be the following :
string="1.2345E-67"
if [[ "$string" =~ ^-?[0-9]*[.,]?[0-9]*[eE]?-?[0-9]+$ ]]
then
echo $string is a number
else
echo $string is not a number
fi
Thus leading to a way to differentiate types of number, if you are looking for any specific type :
string="-12,345"
if [[ "$string" =~ ^-?[0-9]+$ ]]
then
echo $string is an integer
elif [[ "$string" =~ ^-?[0-9]*[.,]?[0-9]*$ ]]
then
echo $string is a float
elif [[ "$string" =~ ^-?[0-9]*[.,]?[0-9]*[eE]-?[0-9]+$ ]]
then
echo $string is a scientific number
else
echo $string is not a number
fi
Note: We could list the syntactical requirements for decimal and scientific notation, one being to allow comma as radix point, as well as ".". We would then assert that there must be only one such radix point. There can be two +/- signs in an [Ee] float. I have learned a few more rules from Aulu's work, and tested against bad strings such as '' '-' '-E-1' '0-0'. Here are my regex/substring/expr tools that seem to be holding up:
parse_num() {
local r=`expr "$1" : '.*\([.,]\)' 2>/dev/null | tr -d '\n'`
nat='^[+-]?[0-9]+[.,]?$' \
dot="${1%[.,]*}${r}${1##*[.,]}" \
float='^[\+\-]?([.,0-9]+[Ee]?[-+]?|)[0-9]+$'
[[ "$1" == $dot ]] && [[ "$1" =~ $float ]] || [[ "$1" =~ $nat ]]
} # usage: parse_num -123.456
[[ $1 =~ ^-?[0-9]+$ ]] && echo "number"
Don't forget -
to include negative numbers!
=~
existed at least as far back as bash 3.0.
– Gilles 'SO- stop being evil'
Aug 22 '14 at 22:45
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_03.html
You can also use bash's character classes.
if [[ $VAR = *[[:digit:]]* ]]; then
echo "$VAR is numeric"
else
echo "$VAR is not numeric"
fi
Numerics will include space, the decimal point, and "e" or "E" for floating point.
But, if you specify a C-style hex number, i.e. "0xffff" or "0XFFFF", [[:digit:]] returns true. A bit of a trap here, bash allows you do to something like "0xAZ00" and still count it as a digit (isn't this from some weird quirk of GCC compilers that let you use 0x notation for bases other than 16???)
You might want to test for "0x" or "0X" before testing if it's a numeric if your input is completely untrusted, unless you want to accept hex numbers. That would be accomplished by:
if [[ ${VARIABLE:1:2} = "0x" ]] || [[ ${VARIABLE:1:2} = "0X" ]]; then echo "$VAR is not numeric"; fi
[[ $VAR = *[[:digit:]]* ]]
will return true if the variable contains a number, not if it is an integer.
– glenn jackman
Oct 26 '12 at 14:48
[[ "z3*&" = *[[:digit:]]* ]] && echo "numeric"
prints numeric
. Tested in bash version 3.2.25(1)-release
.
– Jdamian
Sep 8 '16 at 7:13
[[ -n $VAR && $VAR != *[^[:digit:]]* ]]
– eschwartz
Oct 23 '18 at 15:16
The simplest way is to check whether it contains non-digit characters. You replace all digit characters with nothing and check for length. If there's length it's not a number.
if [[ ! -n ${input//[0-9]/} ]]; then
echo "Input Is A Number"
fi
As i had to tamper with this lately and like karttu's appoach with the unit test the most. I revised the code and added some other solutions too, try it out yourself to see the results:
#!/bin/bash
# N={0,1,2,3,...} by syntaxerror
function isNaturalNumber()
{
[[ ${1} =~ ^[0-9]+$ ]]
}
# Z={...,-2,-1,0,1,2,...} by karttu
function isInteger()
{
[[ ${1} == ?(-)+([0-9]) ]]
}
# Q={...,-½,-¼,0.0,¼,½,...} by karttu
function isFloat()
{
[[ ${1} == ?(-)@(+([0-9]).*([0-9])|*([0-9]).+([0-9]))?(E?(-|+)+([0-9])) ]]
}
# R={...,-1,-½,-¼,0.E+n,¼,½,1,...}
function isNumber()
{
isNaturalNumber $1 || isInteger $1 || isFloat $1
}
bools=("TRUE" "FALSE")
int_values="0 123 -0 -123"
float_values="0.0 0. .0 -0.0 -0. -.0 \
123.456 123. .456 -123.456 -123. -.456 \
123.456E08 123.E08 .456E08 -123.456E08 -123.E08 -.456E08 \
123.456E+08 123.E+08 .456E+08 -123.456E+08 -123.E+08 -.456E+08 \
123.456E-08 123.E-08 .456E-08 -123.456E-08 -123.E-08 -.456E-08"
false_values="blah meh mooh blah5 67mooh a123bc"
for value in ${int_values} ${float_values} ${false_values}
do
printf " %5s=%-30s" $(isNaturalNumber $value) ${bools[$?]} $(printf "isNaturalNumber(%s)" $value)
printf "%5s=%-24s" $(isInteger $value) ${bools[$?]} $(printf "isInteger(%s)" $value)
printf "%5s=%-24s" $(isFloat $value) ${bools[$?]} $(printf "isFloat(%s)" $value)
printf "%5s=%-24s\n" $(isNumber $value) ${bools[$?]} $(printf "isNumber(%s)" $value)
done
So isNumber() includes dashes, commas and exponential notation and therefore returns TRUE on integers & floats where on the other hand isFloat() returns FALSE on integer values and isInteger() likewise returns FALSE on floats. For your convenience all as one liners:
isNaturalNumber() { [[ ${1} =~ ^[0-9]+$ ]]; }
isInteger() { [[ ${1} == ?(-)+([0-9]) ]]; }
isFloat() { [[ ${1} == ?(-)@(+([0-9]).*([0-9])|*([0-9]).+([0-9]))?(E?(-|+)+([0-9])) ]]; }
isNumber() { isNaturalNumber $1 || isInteger $1 || isFloat $1; }
function
keyword as it doesn't do anything useful. Also, I'm not sure about the usefulness of the return values. Unless otherwise specified, the functions will return the exit status of the last command, so you don't need to return
anything yourself.
– Tom Fenech
Sep 9 '16 at 16:39
return
s are confusing and make it less readable. Using function
keywords or not is more a question of personal flavor at least i removed them from the one liners to save some space. thx.
– 3ronco
Sep 9 '16 at 17:07
I use expr. It returns a non-zero if you try to add a zero to a non-numeric value:
if expr -- "$number" + 0 > /dev/null 2>&1
then
echo "$number is a number"
else
echo "$number isn't a number"
fi
It might be possible to use bc if you need non-integers, but I don't believe bc
has quite the same behavior. Adding zero to a non-number gets you zero and it returns a value of zero too. Maybe you can combine bc
and expr
. Use bc
to add zero to $number
. If the answer is 0
, then try expr
to verify that $number
isn't zero.
expr -- "$number" + 0
; yet this will still pretend that 0 isn't a number
. From man expr
: Exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null or 0,
– gniourf_gniourf
Feb 13 '15 at 21:23
For my problem, I only needed to ensure that a user doesn't accidentally enter some text thus I tried to keep it simple and readable
isNumber() {
(( $1 )) 2>/dev/null
}
According to the man page this pretty much does what I want
If the value of the expression is non-zero, the return status is 0
To prevent nasty error messages for strings that "might be numbers" I ignore the error output
$ (( 2s ))
bash: ((: 2s: value too great for base (error token is "2s")
I use printf as other answers mentioned, if you supply the format string "%f" or "%i" printf will do the checking for you. Easier than reinventing the checks, the syntax is simple and short and printf is ubiquitous. So its a decent choice in my opinion - you can also use the following idea to check for a range of things, its not only useful for checking numbers.
declare -r CHECK_FLOAT="%f"
declare -r CHECK_INTEGER="%i"
## <arg 1> Number - Number to check
## <arg 2> String - Number type to check
## <arg 3> String - Error message
function check_number() {
local NUMBER="${1}"
local NUMBER_TYPE="${2}"
local ERROR_MESG="${3}"
local -i PASS=1
local -i FAIL=0
case "${NUMBER_TYPE}" in
"${CHECK_FLOAT}")
if ((! $(printf "${CHECK_FLOAT}" "${NUMBER}" &>/dev/random;echo $?))); then
echo "${PASS}"
else
echo "${ERROR_MESG}" 1>&2
echo "${FAIL}"
fi
;;
"${CHECK_INTEGER}")
if ((! $(printf "${CHECK_INTEGER}" "${NUMBER}" &>/dev/random;echo $?))); then
echo "${PASS}"
else
echo "${ERROR_MESG}" 1>&2
echo "${FAIL}"
fi
;;
*)
echo "Invalid number type format: ${NUMBER_TYPE} to check_number()." 1>&2
echo "${FAIL}"
;;
esac
}
>$ var=45
>$ (($(check_number $var "${CHECK_INTEGER}" "Error: Found $var - An integer is required."))) && { echo "$var+5" | bc; }
This can be achieved by using grep
to see if the variable in question matches an extended regular expression.
1120
:yournumber=1120
if [ $(echo "$yournumber" | grep -qE '^[0-9]+$'; echo $?) -ne "0" ]; then
echo "Error: not a number."
else
echo "Valid number."
fi
Output: Valid number.
1120a
:yournumber=1120a
if [ $(echo "$yournumber" | grep -qE '^[0-9]+$'; echo $?) -ne "0" ]; then
echo "Error: not a number."
else
echo "Valid number."
fi
Output: Error: not a number.
grep
, the -E
switch allows us to use extended regular expression '^[0-9]+$'
. This regular expression means the variable should only []
contain the numbers 0-9
zero through nine from the ^
beginning to the $
end of the variable and should have at least +
one character.grep
, the -q
quiet switch turns off any output whether or not it finds anything.$?
is exit status of the previous executed command. Exit status 0
means success and anything greater means an error. The grep
command has an exit status of 0
if it finds a match and 1
when it doesn't;$()
is a subshell which allows us to execute another command and then use the output.So putting it all together, in a $()
subshell, we echo
the variable $yournumber
and |
pipe it to grep
which with the -q
switch silently matches the -E
extended regular expression '^[0-9]+$'
expression. We then echo
the $?
exit status, which would be 0
if grep
successfully found a match and 1
if it didn't.
Now, outside of the $()
subshell and back in the if
conditional, we take the output, either 0
or 1
from the $()
subshell and check if it is -ne
not equal to "0"
. If it failed to match, the exit status will be 1
which does not match "0"
. Then, we will echo "Error: not a number."
. If succeeded to match, the exit status output would be 0
which is equal to "0"
and in that else case we echo "Valid number."
.
We can just change the regular expression from '^[0-9]+$'
to '^[0-9]*+\.?[0-8]+$'
for floats or doubles.
1120.01
:yournumber=1120.01
if [ $(echo "$yournumber" | grep -qE '^[0-9]*+\.?[0-8]+$'; echo $?) -ne "0" ]; then
echo "Error: not a number."
else
echo "Valid number."
fi
Output: Valid number.
11.20.01
:yournumber=11.20.01
if [ $(echo "$yournumber" | grep -qE '^[0-9]*+\.?[0-8]+$'; echo $?) -ne "0" ]; then
echo "Error: not a number."
else
echo "Valid number."
fi
Output: Error: not a number.
To allow negative integers, just change the regular expression from '^[0-9]+$'
to '^\-?[0-9]+$'
.
To allow negative floats or doubles, just change the regular expression from '^[0-9]*+\.?[0-8]+$'
to '^\-?[0-9]*+\.?[0-8]+$'
.
To catch negative numbers:
if [[ $1 == ?(-)+([0-9.]) ]]
then
echo number
else
echo not a number
fi
When the ‘==’ and ‘!=’ operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below in Pattern Matching, as if the extglob shell option were enabled.
gnu.org/software/bash/manual/bashref.html#index-_005b_005b
– Badr Elmers
May 13 at 3:38
You could use "let" too like this :
[ ~]$ var=1
[ ~]$ let $var && echo "It's a number" || echo "It's not a number"
It\'s a number
[ ~]$ var=01
[ ~]$ let $var && echo "It's a number" || echo "It's not a number"
It\'s a number
[ ~]$ var=toto
[ ~]$ let $var && echo "It's a number" || echo "It's not a number"
It\'s not a number
[ ~]$
But I prefer use the "=~" Bash 3+ operator like some answers in this thread.
I like Alberto Zaccagni's answer.
if [ "$var" -eq "$var" ] 2>/dev/null; then
Important prerequisites: - no subshells spawned - no RE parsers invoked - most shell applications don't use real numbers
But if $var
is complex (e.g. an associative array access), and if the number will be a non-negative integer (most use-cases), then this is perhaps more efficient?
if [ "$var" -ge 0 ] 2> /dev/null; then ..
printf '%b' "-123\nABC" | tr '[:space:]' '_' | grep -q '^-\?[[:digit:]]\+$' && echo "Integer." || echo "NOT integer."
Remove the -\?
in grep matching pattern if you don't accept negative integer.
Did the same thing here with a regular expression that test the entire part and decimals part, separated with a dot.
re="^[0-9]*[.]{0,1}[0-9]*$"
if [[ $1 =~ $re ]]
then
echo "is numeric"
else
echo "Naahh, not numeric"
fi
.
– gniourf_gniourf
May 6 '18 at 22:33
a=''
and the string that contains a period only a='.'
so your code is a bit broken...
– gniourf_gniourf
May 8 '18 at 22:58
I use the following (for integers):
## ##### constants
##
## __TRUE - true (0)
## __FALSE - false (1)
##
typeset -r __TRUE=0
typeset -r __FALSE=1
## --------------------------------------
## isNumber
## check if a value is an integer
## usage: isNumber testValue
## returns: ${__TRUE} - testValue is a number else not
##
function isNumber {
typeset TESTVAR="$(echo "$1" | sed 's/[0-9]*//g' )"
[ "${TESTVAR}"x = ""x ] && return ${__TRUE} || return ${__FALSE}
}
isNumber $1
if [ $? -eq ${__TRUE} ] ; then
print "is a number"
fi
-n
, etc. (because of echo
), and you're accepting variables with trailing newlines (because of $(...)
). And by the way, print
is not a valid shell command.
– gniourf_gniourf
Feb 13 '15 at 21:40
I tried ultrasawblade's recipe as it seemed the most practical to me, and couldn't make it work. In the end i devised another way though, based as others in parameter substitution, this time with regex replacement:
[[ "${var//*([[:digit:]])}" ]]; && echo "$var is not numeric" || echo "$var is numeric"
It removes every :digit: class character in $var and checks if we are left with an empty string, meaning that the original was only numbers.
What i like about this one is its small footprint and flexibility. In this form it only works for non-delimited, base 10 integers, though surely you can use pattern matching to suit it to other needs.
Quick & Dirty: I know it's not the most elegant way, but I usually just added a zero to it and test the result. like so:
function isInteger {
[ $(($1+0)) != 0 ] && echo "$1 is a number" || echo "$1 is not a number"
}
x=1; isInteger $x
x="1"; isInteger $x
x="joe"; isInteger $x
x=0x16 ; isInteger $x
x=-32674; isInteger $x
$(($1+0)) will return 0 or bomb if $1 is NOT an integer. for Example:
function zipIt { # quick zip - unless the 1st parameter is a number
ERROR="not a valid number. "
if [ $(($1+0)) != 0 ] ; then # isInteger($1)
echo " backing up files changed in the last $1 days."
OUT="zipIt-$1-day.tgz"
find . -mtime -$1 -type f -print0 | xargs -0 tar cvzf $OUT
return 1
fi
showError $ERROR
}
NOTE: I guess I never thought to check for floats or mixed types that will make the entire script bomb... in my case, I didn't want it go any further. I'm gonna play around with mrucci's solution and Duffy's regex - they seem the most robust within the bash framework...
1+1
, but rejects some positive integers with leading 0
s (because 08
is an invalid octal constant).
– Gilles 'SO- stop being evil'
Jan 3 '12 at 17:20
0
is not a number, and it is subject to arbitrary code injection, try it: isInteger 'a[$(ls)]'
. Ooops.
– gniourf_gniourf
Feb 14 '15 at 10:36
$((...))
is unquoted, a numeric IFS=123
will change it.
– Isaac
Nov 19 '18 at 21:56
I found quite a short version:
function isnum()
{
return `echo "$1" | awk -F"\n" '{print ($0 != $0+0)}'`
}
"0"
?
– naught101
May 30 '12 at 4:43
test && echo "foo" && exit 0 || echo "bar" && exit 1
approach you're using may have some unintended side effects -- if the echo fails (perhaps output is to a closed FD), theexit 0
will be skipped, and the code will then try toecho "bar"
. If it fails at that too, the&&
condition will fail, and it won't even executeexit 1
! Using actualif
statements rather than&&
/||
is less prone to unexpected side effects. – Charles Duffy Aug 24 '11 at 14:12[[ $1 =~ "^[0-9]+$" ]] && { echo "number"; exit 0; } || { echo "not a number"; exit 1; }
The curly brackets indicate that things should NOT be executed in a subshell (which would definitely be that way with()
parentheses used instead). Caveat: Never miss the final semicolon. Otherwise you might causebash
to print out the ugliest (and most pointless) error messages... – syntaxerror Jun 9 '15 at 17:34[[ 12345 =~ ^[0-9]+$ ]] && echo OKKK || echo NOOO
– Treviño Sep 10 '15 at 10:56