Bash how to check if a string is a number(can be negative) -


this question has answer here:

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

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -