missing better extglob description; missing shell options to control matching, e.g. nocalseglob etc..
A pattern is a string description. Bash can use it in various situations:
The pattern description language is relatively easy. Any character that's not mentioned below matches itself.
The NUL character may not occur in a pattern. If special characters are quoted, they're matched literally without their special meaning.
| Sequence | Description |
|---|---|
* | Matches any string, including the null string (empty string) |
? | Matches any single character |
X | Matches the character X which can be any character that has no special meaning |
\X | Matches the character X, where the character's special meaning is taken away using the backslash |
\\ | Matches a backslash |
[...] | Defines a pattern bracket expression (see below). Matches any of the enclosed characters at this position. |
The bracket expression [...] mentioned above has some useful specialities:
| Bracket expression | Description |
|---|---|
[XYZ] | The "normal" bracket expression, matching either X, Y or Z |
[X-Z] | A range expression: Matching all the characters from X to Y (whatever that means in your current locale, it depends how the characters are sorted!) |
[[:class:]] | Matches all the characters defined by a POSIX® character class: alnum, alpha, ascii, blank, cntrl, digit, graph, lower, print, punct, space, upper, word and xdigit |
[^...] | A negating expression: It matches all the characters that are not in the bracket expression |
[!...] | Equivalent to [^...] |
[]...] or [-...] | Used to include the characters ] and - into the set, they need to be the first characters after the opening bracket |
[=C=] | Matches any character that is eqivalent to the collation weight of C (current locale!) |
[[.SYMBOL.]] | Matches the collating symbol SYMBOL |
Some simple examples using normal pattern matching:
"Hello world" matchesHello world[Hh]"ello world" matchesHello worldhello worldHello* matches (for example)Hello worldHelloworldHelloWoRlDHelloHello world[[:punct:]] matches (for example)Hello world!Hello world.Hello world+Hello world?[[.backslash.]]Hello[[.vertical-line.]]world[[.exclamation-mark.]] matches (using collation sybols)\Hello|world!
If you set the shell option extglob, Bash understands some more powerful patterns. Here, a <PATTERN-LIST> is one or more pattern, separated by the pipe-symbol (|).
?(<PATTERN-LIST>) | Matches zero or one occurrence of the given patterns |
*(<PATTERN-LIST>) | Matches zero or more occurrences of the given patterns |
+(<PATTERN-LIST>) | Matches one or more occurrences of the given patterns |
@(<PATTERN-LIST>) | Matches one of the given patterns |
!(<PATTERN-LIST>) | Matches anything except one of the given patterns |
Delete all but one specific file
rm -f !(survivior.txt)
| option | classification | description |
|---|---|---|
dotglob | globbing | see Pathname expansion customization |
extglob | global | enable/disable extended pattern matching language, as described above |
failglob | globbing | see Pathname expansion customization |
nocaseglob | globbing | see Pathname expansion customization |
nocasematch | pattern/string matching | perform pattern matching without regarding the case of individual letters |
nullglob | globbing | see Pathname expansion customization |
Discussion