9.7. Shell Metacharacters

 <  Day Day Up  >  

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.


 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net