find works for local files that exist but not for remote files or any other time the file doesn't exist locally. Sometimes you want to check for an arbitrary match and the pattern is in a string.
bash supports [[ "$string" == shellmatch* ]]
and [[ "$string" =~ ^regexmatch.* ]]
but does not support any form of [[ "$string" == "$shellmatch" ]]
where the match parameter is a string. I haven't found any way to trick bash into treating a string short of writing out a new shell script properly formatted and I'm not convinced spaces or ]]brackets would be correctly supported.
At Rich’s sh (POSIX shell) tricks is a one liner that works with most POSIX shells using no more than case and some clever quoting. Tested to work with bash, sh. Tested to NOT work with zsh.
fnmatch () { case "$2" in $1) return 0 ;; *) return 1 ;; esac ; }
I use this variant so I can get blank to match all
fnmatch () { [ -z "$1" ] && return 0; case "$2" in $1) return 0 ;; esac; return 1 ; }
Examples:
fnmatch 'ab \?def' 'ab ?def' && echo 'matched' # \ supported
fnmatch 'ab ?def' 'ab cdef' && echo 'matched' # ? supported
fnmatch 'ab [cd]def' 'ab cdef' && echo 'matched' # [] supported
fnmatch 'ab {c,d}def' 'ab cdef' && echo 'matched' # case does not support brace expansion
fnmatch 'ab c*' 'ab cdef' && echo 'matched' # space supported
fnmatch 'ab) c*' 'ab) cdef' && echo 'matched' # ) supported, does not interfere with case