If you ran your script with bash and in exactly the way you show, then the ??? pattern would not be expanded to any existing filenames in the current directory. Can you show some sort of transcript of this actually happening?
– Kusalananda♦Dec 9 '19 at 16:41

Oh, I think I have confused myself a little bit - I was simply adding echo $pattern before the comparison and there it was outputting lib, but it seems that the comparison actually did succeed.
– TylerAndFriendsDec 9 '19 at 16:45

1 Answer
1

is fine as that ??? is quoted, so not expanded by your shell. Note however that the \, $ and ` characters are still special within double quotes. You may want to use single quotes, inside which no character is special (in Bourne-like shells).

One thing you could do here though is remove the bash dependency and do it with standard sh syntax:

Note that while a 0exit status means true, it's more customary to use a 0 number for false and non-zero for true.

Note however that with sh, to be portable, [^x] needs to be written [!x] as [^x] is still not standard.

And whether \ in $pattern is treated specially by the pattern matching code also depends on the implementation and version. that-script '\foo' '\*' may return false on some and true on others. Same forthat-script '*' '\*'.

To match on a literal backslash, use that-script '\foo' '[\\]*'. To match on a wildcard character (?, *, [), use that-script '*?[' '[*][?][[]'. Also beware of a bug/misfeature in the Bourne shell and its descendants (ksh88, ksh93): both that-script a '[a]' and that-script '[a]' '[a]' would return true on systems where /bin/sh is based on AT&T ksh.

I think I am getting myself confused about expansion because I am trying to solve debug why the following case returns "no match": ./wildcart_test.sh "\\" "\\" I started adding echo statements and in those statements the expansion was happening. I am still unsure why the double backslashes do not match.
– TylerAndFriendsDec 9 '19 at 16:49