Bash how to check if a string is a number(can be negative) -
this question has answer here:
- how test if variable number in bash? 34 answers
i want check if string number, tried way:
if ${string} == *[!0-9]* else echo number
but when have negative number, says it's not number, , want work negative numbers too.
try script
#!/bin/bash string=$1 if [[ "$string" =~ ^(-)?[0-9]+$ ]]; echo 'number' else echo 'not number' fi
this works integers.
if want match , decimal number use test
"$string" =~ ^-?[0-9]+(\.[0-9]+)?$
Comments
Post a Comment