[Chapter 1] 1.18 Who Handles Wildcards?
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:31:22Z

#

| Chapter 1 Introduction |

## 1.18 Who Handles Wildcards?

Wildcards (1.16) are actually defined by the UNIX shells, rather than the UNIX filesystem. In theory, a new shell could define new wildcards, and consequently, we should discuss wildcarding when we discuss the shell. In practice, all UNIX shells (including ksh, bash, and other variants (1.8)) honor the same wildcard conventions, and we don't expect to see anyone change the rules. (But different shells do different things when a wildcard doesn't match (15.4).)

You may see different wildcarding if you buy a special-purpose shell that emulates another operating system (for example, a shell that looks like DEC's DCL)-in this case, your shell will obey the other operating system's wildcard rules. But even in this case, operating system designers stick to a reasonably similar set of wildcard rules.

The fact that the shell defines wildcards, rather than the filesystem itself or the program you're running, has some important implications for a few commands. Most of the time, a program never sees wildcards. For example, typing:

>     % lpr *

is exactly the same as typing:

>     % lpr file1 file2 file3 file4 file5

In this case everything works as expected. But there are other situations in which wildcards don't work at all. Assume you want to read some files from a tape, which requires the command tar x (20.4), so you type the command `tar x *.txt`. Will you be happy or disappointed?

You'll be disappointed - unless older versions of the files you want are already in your current directory (1.21). The shell expands the wildcard `*.txt`, according to what's in the current directory, before it hands the completed command line over to tar for execution. All tar gets is a list of files. But you're probably not interested in the current directory; you probably want the wildcard `*` to be expanded on the tape, retrieving any `*.txt` files that the tape has.

There's a way to pass wildcards to programs, without having them interpreted by the shell. Simply put `*.txt` in quotes (8.14). The quotes prevent the UNIX shell from expanding the wildcard, passing it to the command unchanged. Programs that can be used in this way (like uucp and rcp (1.33)) know how to handle wildcards, obeying the same rules as the shell (in fact, these programs usually start a shell to interpret their arguments). You only need to make sure that the programs see the wildcards, that they aren't stripped by the shell before it passes the command line to the program. As a more general rule, you should be aware of when and why a wildcard gets expanded, and you should know how to make sure that wildcards are expanded at an appropriate time.

> NOTE: If your shell understands the `{}` characters (9.5), you can use them because they can generate any string - not just filenames that already exist. You have to type the unique part of each name, but you only have to type the common part once. For example, to extract the files called project/wk9/summary, project/wk14/summary, and project/wk15/summary from a tar tape, you might use:

> >     % tar xv project/wk{9,14,15}/summary
> >     x project/wk9/summary, 3161 bytes, 7 tape blocks
> >     x project/wk14/summary, 878 bytes, 2 tape blocks
> >     x project/wk15/summary, 2268 bytes, 5 tape blocks

Some versions of tar understand wildcards, but many don't. There is a clever workaround (20.9).

- ML

1.17 Filename Extensions |  | 1.19 The Tree Structure of the Filesystem