10.5. Metacharacters

Metacharacters are special characters that are used to represent something other than themselves. As a rule of thumb, characters that are neither letters nor numbers may be metacharacters. The shell has its own set of metacharacters, often called shell wildcards. Shell metacharacters can be used to group commands together, to abbreviate filenames and pathnames, to redirect and pipe input/output, to place commands in the background, and so forth. Table 10.13 presents a partial list of shell metacharacters.

Table 10.13. Shell Metacharacters
Metacharacter Purpose Example Meaning
$ Variable substitution set name=Tom echo $name Tom Sets the variable name to Tom; displays the value stored there.
! History substitution !3 Reexecutes the third event from the history list.
* Filename substitution rm * Removes all files.
? Filename substitution ls ?? Lists all two character files.
[ ] Filename substitution cat f[123] Displays contents of f1, f2, f3.
; Command separator ls;date;pwd Each command is executed in turn.
& Background processing lp mbox& Printing is done in the background. Prompt returns immediately.
> Redirection of output ls > file Redirects standard output to file.
< Redirection of input ls < file Redirects standard input from file.
>& Redirection of output and error ls >& file Redirects both output and errors to file.
>! If noclobber is set, override it ls >! file If file exists, truncate and overwrite it, even if noclobber is set.
>>! If noclobber is set, override it ls >>! file If file does not exist, create it, even if noclobber is set.
( ) Groups commands to be executed in a subshell (ls ; pwd) >tmp Executes commands and sends output to tmp file.
{ } Groups commands to be executed in this shell { cd /; echo $cwd } Changes to root directory and displays current working directory.

10.5.1. Filename Substitution

When evaluating the command line, the shell uses metacharacters to abbreviate filenames or pathnames that match a certain set of characters. The filename substitution metacharacters listed in Table 10.14 are expanded into an alphabetically listed set of filenames. The process of expanding a metacharacter into filenames is also called globbing. Unlike the other shells, when the C shell cannot substitute a filename for the metacharacter it is supposed to represent, the shell reports "No match."

Table 10.14. Shell Metacharacters and Filename Substitution
Metacharacter Meaning
* Matches zero or more characters
? Matches exactly one character
[abc] Matches one character in the set: a, b, or c
[a-z] Matches one character in the range a to z
[^abc] Matches any character that is not a, b, or c
{a, ile, ax} Matches for a character or set of characters
~ Substitutes the user's home directory for tilde
Escapes or disables the metacharacter

Expanding the Metacharacters

The shell performs filename substitution by evaluating its metacharacters and replacing them with the appropriate letters or digits in a filename.

The Asterisk

The asterisk matches zero or more characters in a filename.

Example 10.44.
1  >  ls
							a.c b.c abc ab3 file1 file2 file3 file4 file5

2  >  echo *
							a.c b.c abc ab3 file1 file2 file3 file4 file5

3  >  ls  *.c
							a.c b.c

4  > ls ^*.c
							abc ab3 file1 file2 file3 file4 file5

5  > rm  z*p
							No match.
						

Explanation

  1. All the files in the current directory are listed.

  2. The echo program prints all its arguments to the screen. The asterisk (also called a splat) is a wildcard that means "match for zero or more of any characters found in a filename." All the files in the directory are matched and echoed to the screen.

  3. Filenames ending in .c are listed.

  4. All files not ending in .c are listed.

  5. Because none of the files in the directory start with z, the shell reports "No match."

The Question Mark

The question mark matches exactly one character in a filename.

Example 10.45.
1  > ls
							a.c b.c abc ab3 file1 file2 file3 file4 file5

2  > ls ???
							abc ab3

3  > echo How are you?
							No match.

4  > echo How are you?
							How are you?
						

Explanation

  1. All the files in the current directory are listed.

  2. The question mark matches for a single-character filename. Any filenames consisting of three characters are listed.

  3. The shell looks for a filename spelled y-o-u followed by one character. There is not a file in the directory that matches these characters. The shell prints "No match."

  4. The backslash preceding the question mark is used to turn off the special meaning of the question mark. Now the shell treats the question mark as a literal character.

The Square Brackets

The square brackets match a filename for one character from a set or range of characters.

Example 10.46.
1  > ls
							a.c b.c abc ab3 file1 file2 file3 file4 file5 file10  file11  file12

2  > ls file[123]
							file1 file2 file3

3  > ls file[^123]
							file4 file5

4  > ls [A-Za-z][a-z][1-5]
							ab3

5  >  ls file1[0-2]
							file10 file11 file12
						

Explanation

  1. All the files in the current directory are listed.

  2. Filenames ending in file followed by 1, 2, or 3 are matched and listed.

  3. Filenames ending in file followed by 1, 2, or 3 are matched and listed.

  4. Filenames starting with one capital letter, followed by one lowercase letter, and followed by one number are matched and listed.

  5. Filenames starting with file1 and followed by a 0, 1, or 2 are listed.

The Curly Braces

The curly braces match for a character or string of characters in a filename that may or may not already exist.

Example 10.47.
1  > ls
							a.c b.c abc ab3 ab4 ab5 file1 file2 file3 file4 file5 foo
							faa fumble

2  > ls f{oo,aa,umble}
							foo faa fumble

3  > ls a{.c,c,b[3-5]}
							a.c ab3 ab4 ab5

4  > mkdir prog{1,2,3}

5  > echo tweedle{dee,dum}, {l,cl,m}ove{r}
							tweedledee tweedledum, lover clover mover
						

Explanation

  1. All the files in the current directory are listed.

  2. Files starting with f and followed by the strings oo, aa, or umble are listed. Spaces inside the curly braces will cause the error message Missing }.

  3. Files starting with a followed by .c, c, or b3, b4, or b5 are listed. (The square brackets can be used inside the curly braces.)

  4. Rather than typing prog1, prog2, and prog3 and separate arguments to the mkdir command, you can use the braces to generate the new directory names.

  5. Each of the words is expanded using the braced portions either as prefixes or suffixes. It is important that the words in braces contain no white space.

Escaping Metacharacters and nonomatch

The backslash is used to escape the special meaning of a single character. The escaped character will represent itself.

Example 10.48.
1  > got milk?
							got: No match.

2  > got milk?
							got: Command not found.

3  > set nonomatch
   > got milk?
							got: Command not found.
						

Explanation

  1. The question mark is a file substitution metacharacter and evaluates to a single character. The shell looks for a file in the present working directory that contains the characters m-i-l-k, followed by a single character. If the shell cannot find the file, it reports No match. This shows you something about the order in which the shell parses the command line. The metacharacters are evaluated before the shell tries to locate the got command.

  2. The backslash protects the metacharacter from interpretation, often called escaping the metacharacter. Now the shell does not complain about a No match, but searches the path for the got command, which is not found.

  3. The built-in nonomatch variable, when set, turns off error reporting when a metacharacter is not matched. Instead of reporting No match, Linux reports that got is not a command.

Tilde Expansion

The tilde character by itself expands to the full pathname of the user's home directory. When the tilde is prepended to a username, it expands to the full pathname of that user's home directory. When prepended to a path, it expands to the home directory and the rest of the pathname.

Example 10.49.
1  > echo ~
							/home/jody/ellie

2  >  cd ~/desktop/perlstuff
   > pwd
							/home/jody/ellie/desktop/perlstuff

3  > cd ~joe
   > pwd
							/home/bambi/joe
						

Explanation

  1. The tilde expands to the user's home directory.

  2. The tilde followed by a pathname expands to the user's home directory, followed by /desktop/perlstuff.

  3. The tilde followed by a username expands to the home directory of the user. In this example, the directory is changed to that user's home directory.

Turning Off Metacharacters with noglob

If the noglob variable is set, filename substitution is turned off, meaning that all metacharacters represent themselves; they are not used as wildcards. This can be useful when searching for patterns in programs like grep, sed, or awk, which may contain metacharacters that the shell may try to expand.

Example 10.50.
1  > set noglob
2  > echo * ?? [] ~
							* ?? [] ~
						

Explanation

  1. The variable noglob is set. It turns off the special meaning of the wildcards.

  2. The metacharacters are displayed as themselves without any interpretation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.190