You need to use the =~
operator.
From man bash
:
An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly.
Try this:
foo()
{
[[ "$1" =~ $2 ]] && echo "string '$1' matches pattern '$2'"
}
Also note that *
is a wildcard (and undergoes "pattern matching") while .*
is a regular expression.
Change your example to:
$ foo bar '.*(r|s)'
string 'bar' matches pattern '.*(r|s)'