Version 16 of wild cards

Updated 2002-09-06 12:41:25

In a shell environment, the term wild cards refers to metacharacters that are then expanded (also referenced as globbed) into a series of file names.

See glob for a Tcl function which provides some wild card expansion.

One concept that is sometimes hard for people to grasp is that wild cards are often handled transparently to the user.

That is to say, on Unix, before a command ever sees a command line from a shell, the shell has expanded the wild cards into file names.

However, other shells, such as Tcl, do not do such wild card expansions automatically. The programmer of an application is responsible to either expand them (via glob) or to invoke an external command which then expands the wild cards.


Glob-style wild-card characters

* matches any string of characters (including the empty string)

? matches any single character.

[...] matches a set of characters (need more detail)

{alt1,alt2} matches alternatives (but not in [string match]) (Wiki bug: can't have unbalanced braces inside font context, so alt1 and alt2 are erroneously bold.)

\\ forces the next character to be interpreted as a literal. (Wiki bug: single backslash causes crash.)

ab*cd
Matches a string with ab at the start and cd at the end
ab?cd
Matches a string 5 characters long with ab at the start and cd at the end.
ab[cd]
Matches a string 3 characters long starting with ab followed by c or d
ab[c-e]f
Matches a string 4 characters long starting with ab, followed by a c, d or e and with an f at the end.
   ab{c,?c}d   Matches any string 4 or 5 characters long with ''ab'' at the start and ''cd'' at the end.
a\*c
Matches precisely the string a*c character