Pattern Matching in case Statements
A case statement () is good at string pattern matching. Its "wildcard" pattern-matching metacharacters work like the filename wildcards () in the shell, with a few twists. Here are some examples:
?)- Matches a string with exactly one character like
a, ,!, and so on. ?*)- Matches a string with one or more characters (a non-empty string).
[yY]|[yY][eE][sS])- Matches
y,Yoryes,YES,YeS, etc. The|means "or." /*/*[0-9])- Matches a file pathname, like /xxx/yyy/somedir/file2, that starts with a slash, contains at least one more slash, and ends with a digit.
'What now?')- Matches the pattern
What now?. The quotes () tell the shell to treat the string literally: not to break it at the space and not to treat the?as a wildcard. "$msgs")- Matches the contents of the msgs variable. The double quotes let the shell substitute the variable's value; the quotes also protext spaces and other special characters from the shell. For example, if msgs contains
first next, then this would match the same string,first next.
- JP