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.
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. |
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."
The shell performs filename substitution by evaluating its metacharacters and replacing them with the appropriate letters or digits in a filename.
The asterisk matches zero or more characters in a filename.
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
All the files in the current directory are listed.
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.
Filenames ending in .c are listed.
All files not ending in .c are listed.
Because none of the files in the directory start with z, the shell reports "No match."
The question mark matches exactly one character in a filename.
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
All the files in the current directory are listed.
The question mark matches for a single-character filename. Any filenames consisting of three characters are listed.
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."
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 match a filename for one character from a set or range of characters.
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
All the files in the current directory are listed.
Filenames ending in file followed by 1, 2, or 3 are matched and listed.
Filenames ending in file followed by 1, 2, or 3 are matched and listed.
Filenames starting with one capital letter, followed by one lowercase letter, and followed by one number are matched and listed.
The curly braces match for a character or string of characters in a filename that may or may not already exist.
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
All the files in the current directory are listed.
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 }.
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.
The backslash is used to escape the special meaning of a single character. The escaped character will represent itself.
1 > got milk? got: No match. 2 > got milk? got: Command not found. 3 > set nonomatch > got milk? got: Command not found. |
Explanation
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.
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.
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.
1 > echo ~ /home/jody/ellie 2 > cd ~/desktop/perlstuff > pwd /home/jody/ellie/desktop/perlstuff 3 > cd ~joe > pwd /home/bambi/joe |
Explanation
The tilde expands to the user's home directory.
The tilde followed by a pathname expands to the user's home directory, followed by /desktop/perlstuff.
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.
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.
1 > set noglob 2 > echo * ?? [] ~ * ?? [] ~ |
Explanation
The variable noglob is set. It turns off the special meaning of the wildcards.
The metacharacters are displayed as themselves without any interpretation.
3.137.223.190