home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 8.8 A Directory for Commands You Shouldn't Run Chapter 8
How the Shell Interprets What You Type
Next: 8.10 eval: When You Need Another Chance
 

8.9 Wildcards Inside of Aliases

Here's another example in which command-line parsing is important. Consider this alias for counting the number of words in all files:

wc
 
% alias words "wc -w *"

Right away, we can see one effect of command-line parsing. The shell sees the quotation marks, and knows not to expand wildcards inside the quotation marks. Therefore, words is aliased to wc -w * ; the * isn't evaluated when you create the alias. (If wildcards were processed before quotes, this won't work.)

Now, think about what happens when you execute the alias. You type:

% words

The shell starts working through its steps (8.5 ) , and eventually performs alias substitution. When this happens, it converts your command into:

wc -w *

Now, watch carefully. The shell continues working through the process of interpretation (redirection, variable substitution, command substitution), and eventually gets to filename expansion. At this point, the shell sees the * on the command line, expands it, and substitutes the files in the current directory. Seems simple enough. But think: you didn't type this * ; the shell put it there when it expanded the wildcard. What would have happened if the shell expanded wildcards before substituting aliases? The * would never have been expanded; by the time the shell put it on the command line, the wildcard expansion stage would be over, and you'd just count the words in a file named * (which probably doesn't exist).

To me, the amazing thing is that all this works - and works well! The workings of the command line are intricate and complex, but the shell almost always does what you want - and without a lot of thought.

- ML


Previous: 8.8 A Directory for Commands You Shouldn't Run UNIX Power Tools Next: 8.10 eval: When You Need Another Chance
8.8 A Directory for Commands You Shouldn't Run Book Index 8.10 eval: When You Need Another Chance

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System