Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Quote Wildcards for cpio and find

There are other commands like echo that have their own special characters that must be quoted for the shell to pass them unaltered. cpio is a command that saves and restores files. It enables shell filename wildcards to select the files to restore. These wildcards must be quoted to prevent shell expansion. This enables them to be passed to cpio for interpretation, as in the following example:

cpio -icvdum 'usr2/*' < /dev/rmt0

-icvdum includes options to cpio to specify how it should restore files from the tape device /dev/rmt0. usr2/* says to restore all files from directory usr2 on tape. Again, this command sometimes works correctly even if the wildcards aren’t quoted because shell expansion doesn’t occur if matching files aren’t found in the current path (in this case, if there is no usr2 subdirectory in the current directory). It is best to quote these cpio wildcards so you can be sure the command works properly every time.

The find command is covered in chapter 18, “Miscellaneous Tools.” It supports its own wildcards to look for partial filenames:

find / -name 'ch*.doc' -print

ch*.doc is a wildcard pattern that tells find to display all filenames that start with ch and end with a .doc suffix. Unlike shell filename expansion, this find command checks all directories on the system for a match. However, the wildcard must be quoted using single quotes, double quotes, or a backslash, so the wildcard is passed to find and not expanded by the shell.

Summary

In this chapter, you looked at three types of quoting and when to use them:

  Backslash
  Single quote
  Double quote

Here is a summary of the quoting rules you learned in this chapter in the order of presentation:

  A backslash takes away the special meaning of the character that follows it.
  The character doing the quoting is removed before command execution.
  Single quotes remove the special meaning of all enclosed characters.
  Quoting regular characters is harmless.
  A single quote can not be inserted within single quotes.
  Double quotes remove the special meaning of most enclosed characters.
  Quoting can ignore word boundaries.
  Different types of quoting can be combined in one command.
  Quote spaces to embed them in a single argument.
  Quote the newline to continue a command on the next line.
  Use quoting to access filenames that contain special characters.
  Quote regular expression wildcards.
  Quote the backslash to enable echo escape sequences.
  Quote wildcards for cpio and find.

Questions

1.  Give an echo command to display this message:
       It's <party> time!
2.  Give an echo command to display one line containing the following fields:
  The contents of variable $USER
  A single space
  The word "owes"
  Five spaces
  A dollar sign ($)
  The contents of the variable $DEBT (this variable contains only digits)

Sample output:
fred owes     $25

Terms

Quoting
Quoting literally encloses selected text within some type of quotation marks. When applied to shell commands, quoting means to disable shell interpretation of special characters by enclosing the characters within single or double quotes or by escaping the characters.
Escaping
Escaping a character means to put a backslash (\) just before that character. Escaping can either remove the special meaning of a character in a shell command, or it can add special meaning as we saw with \n in the echo command. The character following the backslash is called an escaped character.
Special characters, metacharacters, wildcards
All these terms indicate characters that are not taken at face value. These characters have an extra meaning or cause some action to be taken by the shell or other UNIX commands.
Literal characters
These characters have no special meaning and cause no extra action to be taken. Quoting causes the shell to treat a wildcard as a literal character.
Newline character
This is literally the linefeed character whose ASCII value is 10. In general, the newline character is a special shell character that indicates a complete command line has been entered and can now be executed.
PS2 variable
This shell variable’s content is usually the > character. The content of the PS2 variable is displayed by the shell as a secondary prompt that indicates the previous command was not complete and the current command line is a continuation of that command line.
Shell preprocessing
This describes actions taken by the shell to manipulate the command line before executing it. This is when filename, variable, command, and arithmetic substitution occur (as covered in chapter 8).


Previous Table of Contents Next