Is there a way to invert this so that it checks if it is invalid first?
if expr "$string" : '-\?[0-9]\+$' >/dev/null
then
echo "String is a valid integer."
else
echo "String is not a valid integer."
fi
Yes, I agree with @nobody and @BenJackson, all you need is to add the 'logical NOT' operator , i.e.
if ! expr "$string" : '-\?[0-9]\+$' >/dev/null ; then
#--^ right there ;-)
echo "String is not a valid integer."
else
echo "String is a valid integer."
fi
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.
if [ ! expr...]?[ ]to use!. You can type something like! falseright in the shell and see it succeeds.