I wanted to publish this summary about wildcards in the bash (and similar shells) some time ago, but didn’t finish it. But finally it gets published.

The shell handles words or patterns containing a wildcard as a template. Available filenames are tested to see if they fit this template. This evaluation is also called globbing. Let’s have a look at a small example:

In this example * is replaced by appropriate characters, and the list of matching files are passed to the ls command. This set of files will be used in the following examples.

## Encode for a single character: ?

The question mark can be replaced by a single character. So if you want to get the files aaa1 , aaa2 , aaa3 and aaab you can use the following pattern:

So you see, the ? is replaced by exactly one character. That is, both aaa and aaaa1 won’t match.

## Encode for a an arbitrary number of characters: *

To match any number of characters you can use the asterix * . It can replace 0 to n characters, n is limited by the max length of the file name and depends on the file system you’re using. Adapting the previous snippet you’ll now also get aaa and aaaa1 :

## Encode for a set of characters: [...]

Most of the common tasks can be done with the previous templates, but there are cases when you need to define the characters that should be replaced. You can specify this set of characters using brackets, e.g. [3421] can be replaced by 3 , 4 , 2 or 1 and is the same as [1-4] :

As you can see aaaa5 doesn’t match [3421] , and btw. the order of the specified characters doesn’t matter. And because it would be very annoying if you want to match against any alphabetic character (you would need to type all 26 characters), you can specify character ranges using a hyphen ( a-z ). Here are some exmaples:

TemplateCharacter set
[xyz1] x , y , z or 1
[C-Fc-f] C , D , E , F , c , d , e or f
[a-z0-9] Any small character or digit
[^b-d] Any character except b , c , d
[Yy][Ee][Ss] Case-insensitive matching of yes
[[:alnum:]] Alphanumeric characters, same as A-Za-z0-9
[[:alpha:]] Alphabetic characters, same as A-Za-z
[[:digit:]] Digits, same as 0-9
[[:lower:]] Lowercase alphabetic characters, same as a-z
[[:upper:]] Uowercase alphabetic characters, same as A-Z
[[:space:]] Whitespace characters (space, tab etc.)

Btw. the files that match such a template are sorted before they are passed to the command.