Previous Section  < Day Day Up >  Next Section

9.7. Shell 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. Like grep, sed, and awk, the shell has its own set of metacharacters, often called shell wildcards.[4] 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 9.3 presents a partial list of shell metacharacters.

[4] Programs such as grep, sed, and awk have a set of metacharacters, called regular expression metacharacters, for pattern matching. These should not be confused with shell metacharacters.

Table 9.3. 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

Re-executes 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.


    Previous Section  < Day Day Up >  Next Section