@scrutinizer - you are correct. Look below.
Actually, my point was not about anything that uses validation. That whole thing was a trip down a rabbit hole.
The OP insisted that there was no way that a variable could be treated as "polymorphic", I showed two shell examples to the contrary. i.e., 01 and 1 would register as 1 in
[ $myvar -eq 1 ]
The only conclusion was that OP was using an ancient shell, so I suggested typeset. Then we all went down a rabbit hole. We are still there.
To answer your question in the OP's context let's see what ksh88 on Solaris 10 "thinks":
for i in "-1" "+1" "01" "1" " 1"
do
printf "%%s=%s: %%d=:%d: " $i $i
[ $i -eq 1 ] && echo "Numeric and equals 1"
[ $i -eq -1 ] && echo "Numeric and equals -1"
[ $i -eq 0 ] && echo "probably not numeric"
done
output:
%s=-1: %d=:-1: Numeric and equals -1
%s=+1: %d=:1: Numeric and equals 1
%s=01: %d=:1: Numeric and equals 1
%s=1: %d=:1: Numeric and equals 1
%s=1: %d=:1: Numeric and equals 1
Now let's compare that to bash and bash plus RE - note change to RE
#!/bin/bash
for i in "-1" "+1" "01" "1" " 1" "-000001"
do
printf "%%s=:%s: %%d=:%d: " $i $i
[ $i -eq 1 ] && echo "Numeric and equals 1"
[ $i -eq -1 ] && echo "Numeric and equals -1"
[ $i -eq 0 ] && echo "probably not numeric"
done
echo 'using RE'
regex='^([+-]{0,1}[0-9]+$| [0-9]+$|[0-9]+$)'
for i in "-1" "+1" "01" "1" " 1" "-0000001"
do
printf "%%s=:%s: %%d=:%d: " $i $i
if [[ $i =~ $regex ]] ; then
echo 'tests numeric '
else
echo 'tests not numeric'
fi
done
output:
%s=:-1: %d=:-1: Numeric and equals -1
%s=:+1: %d=:1: Numeric and equals 1
%s=:01: %d=:1: Numeric and equals 1
%s=:1: %d=:1: Numeric and equals 1
%s=:1: %d=:1: Numeric and equals 1
%s=:-000001: %d=:-1: Numeric and equals -1
using RE
%s=:-1: %d=:-1: tests numeric
%s=:+1: %d=:1: tests numeric
%s=:01: %d=:1: tests numeric
%s=:1: %d=:1: tests numeric
%s=:1: %d=:1: tests numeric
%s=:-0000001: %d=:-1: tests numeric
Hmm. ksh88 is the oldest shell I have. So none of the other stuff is really needed to do what the OP appears to have wanted, IMO. This was my original point. Your point is that signed numbers fail the RE in my example, which is true. My bad. Fixed.
All of this might have been over the OP's level of understanding anyway.