How to Check given string is number in shell script?

Hi,

Can anyone help me out to check whether the input argument is number?

Example:

REQUEST_ID="123456"

I need to check the REQUEST_ID value is number or string.

Thanks in Advance

Regards
BS

 
echo "12345S" | awk '$0 ~/[^0-9]/ { print "non number" }'

Thanks a lot..

Its working fine...

You can also do such checks entirely within your shell script. The following works for ksh93 and also for bash if you enable extglob.

# uncomment if using bash
# shopt -s extglob

REQUEST_ID="123456"

case $REQUEST_ID in
   ( +([0-9]) )   echo "REQUEST_ID is all numbers" ;;
             *)   echo "REQUEST_ID is not all numbers" ;;
esac

How to assign this output into variable

I have tried the below option

X=`echo "$REQUEST_ID" | awk '$0 ~/[^0-9]/ { print TRUE }'`

echo $X

its printing empty string.

Regards
BS

a=`echo $REQUEST_ID| tr -d "[0-9]`
if [[ -z $a ]]; then it would be number
[

]

-----Post Update-----

put quotes i.e. " around TRUE

Thanks for your quick reply... I have fixed the problem

Here the working code snippet

REQUEST_ID="1234"
X=`echo "$REQUEST_ID" | awk '$0 ~/[^0-9]/ { print "NOT_NUMBER" }'`
echo "Request Id: $REQUEST_ID"
echo "TEST :::$X"

if [ "$REQUEST_ID" != "" ] && [ "$X" != "NOT_NUMBER" ]; then
echo "Its number......"
else
echo "Not an number"
fi

Regards
BS

1 Like