SC2081
Mohammad Faisal edited this page Mar 10, 2021
·
2 revisions
[ .. ] can't match globs. Use [[ .. ]] or grep.
Problematic code:
if [ $var == *[^0-9]* ]
then
echo "$var is not numeric"
fi Correct code:
if [[ $var == *[^0-9]* ]]
then
echo "$var is not numeric"
fi Rationale:
[ .. ] aka test can not match against globs.
In bash/ksh, you can instead use [[ .. ]] which supports this behavior.
In sh, you can rewrite to use grep.
if echo $var | grep -q '^[0-9]*$'; then
echo "$var is numeric"
fiExceptions:
None. If you are not trying to match a glob, quote the argument (e.g. [ $var == '*' ] to match literal asterisk.