I want to check if a certain string is a number, I tried this way:
if ${string} == *[!0-9]*
else echo its a number
but when I have a negative number, it says it's not a number, and i want it to work for negative numbers too.
I want to check if a certain string is a number, I tried this way:
if ${string} == *[!0-9]*
else echo its a number
but when I have a negative number, it says it's not a number, and i want it to work for negative numbers too.
Try this script
#!/bin/bash
string=$1
if [[ "$string" =~ ^(-)?[0-9]+$ ]]; then
echo 'Number'
else
echo 'Not number'
fi
This works only for integers.
If you want to match and decimal number then use this test
"$string" =~ ^-?[0-9]+(\.[0-9]+)?$