Reputation: 1966
How can I check if MyVar
contains only digits with an if
statement with BASH. By digits I am referring to 0-9.
ie:
if [[ $MyVar does contain digits ]] <-- How can I check if MyVar is just contains numbers
then
do some maths with $MyVar
else
do a different thing
fi
Upvotes: 28
Views: 50016
Reputation: 621
For Bash, another option, maybe less expensive than regex match (I did not make performance tests), is to use pattern substitution
to replace all digits with empty string, and test if the resulting string is empty.
if [[ -z ${var//[0-9]} ]]; then # or ${var//[[:digit:]]}
echo "only digits"
else
echo "not only digits"
fi
Upvotes: 3
Reputation: 1144
Grep expression solution without [[ ]], for wider shell compatability:
if [ "$(echo $var | grep -E "^[0-9]{1,}$")" ]; then echo "digits only"; then
echo "var contains digits only"
else
echo "var contains digits and/or other characters"
fi
Upvotes: 1
Reputation: 111
Simple! just use expression option in grep The below solution echo's the variable and checks if it contains only digits
if [[ $(echo $var | grep -E "^[[:digit:]]{1,}$") ]]
then
echo "var contains only digits"
else
echo "var contains other characters apart from digits"
fi
Upvotes: 1
Reputation: 84561
If you would like to test in a POSIX compliant way, you can use either:
expr string : regex ## returns length of string if both sides match
or
expr match string regex ## behaves the same
For example to test if $myvar
is all digits:
[ $(expr "x$myvar" : "x[0-9]*$") -gt 0 ] && echo "all digits"
Note: 'x'
prepended to the variable and expression to protect against test of empty-string throwing error. To use the length
returned by the test, don't forget to subtract 1
which represents the 'x'
.
In if-then-else
form, here is a short script that tests whether the first argument to the script contains all digits:
#!/bin/sh
len=$(expr "x$1" : "x[0-9]*$") ## test returns length if $1 all digits
let len=len-1 ## subtract 1 to compensate for 'x'
if [ $len -gt 0 ]; then ## test if $len -gt 0 - if so, all digits
printf "\n '%s' : all digits, length: %d chars\n" "$1" $len
else
printf "\n '%s' : containes characters other than [0-9]\n" "$1"
fi
Example Output
$ sh testdigits.sh 265891
'265891' : all digits, length: 6 chars
$ sh testdigits.sh 265891t
'265891t' : contains characters other than [0-9]
The bash regular expression test [[ $var =~ ^[0-9]+$ ]]
is fine, I use it, but it is a bashism (limited to the bash shell). If you are concerned with portability, the POSIX test will work in any POSIX compliant shell.
Upvotes: 11
Reputation: 113844
[[ $myvar =~ [^[:digit:]] ]] || echo All Digits
Or, if you like the if-then
form:
if [[ $myvar =~ [^[:digit:]] ]]
then
echo Has some nondigits
else
echo all digits
fi
In olden times, we would have used [0-9]
. Such forms are not unicode safe. The modern unicode-safe replacement is [:digit:]
.
Upvotes: 18
Reputation: 3058
Here it is:
#!/bin/bash
if [[ $1 =~ ^[0-9]+$ ]]
then
echo "ok"
else
echo "no"
fi
It prints ok
if the first argument contains only digits and no
otherwise. You could call it with: ./yourFileName.sh inputValue
Upvotes: 41