-
Notifications
You must be signed in to change notification settings - Fork 1.8k
SC3015
Lawrence Velázquez edited this page Mar 9, 2024
·
8 revisions
#!/bin/sh
if [ "$var" =~ foo[0-9]+ ]; then
echo matched
fi
#!/bin/sh
if expr "$var" : '.*foo[0-9]\{1,\}' >/dev/null; then
echo matched
fi
or
#!/bin/sh
case $var in
*foo[0-9]*)
echo matched
;;
esac
You are using =~
in a script declared to be compatible with POSIX sh or Dash, but =~
is not specified by POSIX and is unlikely to work outside [[ ]]
in Bash and Ksh.
Use expr
's :
operator instead. It may be necessary to revise the regular expression because POSIX expr
uses basic regular expressions anchored to the beginning of the string, as opposed to the unanchored extended regular expressions used by [[ str =~ re ]]
in Bash and Ksh.
Alternately, use case
if the matching can be done with shell patterns instead of regular expressions. This avoids the need for an external utility.
None