home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeBook TitleSearch this book

4.2. Syntax

This section describes the many symbols peculiar to the Bourne and Korn shells. The topics are arranged as follows:

  • Special files

  • Filename metacharacters

  • Quoting

  • Command forms

  • Redirection forms

  • Coprocesses (Korn shell only)

4.2.2. Filename Metacharacters

*

Match any string of zero or more characters.

?

Match any single character.

[abc...]

Match any one of the enclosed characters; a hyphen can specify a range (e.g., a–z, A–Z, 0–9).

[!abc...]

Match any character not enclosed as above.

In the Korn shell:

?(pattern)

Match zero or one instance of pattern.

*(pattern)

Match zero or more instances of pattern.

+(pattern)

Match one or more instances of pattern.

@(pattern)

Match exactly one instance of pattern.

!(pattern)

Match any strings that don't match pattern.

\n

Match the text matched by the n'th subpattern in (...). ksh93 only.

~

Home directory of the current user.

~name

Home directory of user name.

~+

Current working directory ($PWD).

~-

Previous working directory ($OLDPWD).

This pattern can be a sequence of patterns separated by |, meaning that the match applies to any of the patterns. If & is used instead of |, all the patterns must match. & has higher precedence than |. This extended syntax resembles that available in egrep and awk.

ksh93 supports the POSIX [[=c=]] notation for matching characters that have the same weight, and [[.c.]] for specifying collating sequences. In addition, character classes, of the form [[:class:]], allow you to match the following classes of characters.

ClassCharacters Matched

alnum

Alphanumeric characters

alpha

Alphabetic characters

blank

Space or tab

cntrl

Control characters

digit

Decimal digits

graph

Nonspace characters

lower

Lowercase characters

print

Printable characters

space

Whitespace characters

upper

Uppercase characters

xdigit

Hexadecimal digits

4.2.3. Quoting

Quoting disables a character's special meaning and allows it to be used literally, as itself. The following table displays characters have special meaning to the Bourne and Korn shells.

CharacterMeaning

;

Command separator

&

Background execution

( )

Command grouping

|

Pipe

< > &

Redirection symbols

* ? [ ] ~ + - @ !

Filename metacharacters

" ' \

Used in quoting other characters

Command substitution

$

Variable substitution (or command or arithmetic substitution)

space tab newline

Word separators

These characters can be used for quoting:

" "
Everything between " and " is taken literally, except for the following characters that keep their special meaning:

$
Variable (or Korn shell command and arithmetic) substitution will occur.

Command substitution will occur.

"
This marks the end of the double quote.

' '
Everything between ' and ' is taken literally except for another '. You cannot embed another ' within such a quoted string.

\
The character following a \ is taken literally. Use within " " to escape ", $, and . Often used to escape itself, spaces, or newlines.

$" "
ksh93 only. Just like "", except that locale translation is done.

$' '
ksh93 only. Similar to '', but the quoted text is processed for the following escape sequences:

Sequence ValueSequenceValue
\a Alert\nnnOctal value nnn
\b Backspace\xnnHexadecimal value nn
\f Form feed\'Single quote
\n Newline\"Double quote
\r Carriage return\\Backslash
\t Tab\EEscape
\v Vertical tab 

4.2.5. Redirection Forms

File DescriptorNameCommon AbbreviationTypical Default

0

Standard input

stdin

Keyboard

1

Standard output

stdout

Terminal

2

Standard error

stderr

Terminal

The usual input source or output destination can be changed, as seen in the following sections.

4.2.5.2. Redirection using file descriptors

cmd >&n

Send cmd output to file descriptor n.

cmd m>&n

Same, except that output that would normally go to file descriptor m is sent to file descriptor n instead.

cmd >&-

Close standard output.

cmd <&n

Take input for cmd from file descriptor n.

cmd m<&n

Same, except that input that would normally come from file descriptor m comes from file descriptor n instead.

cmd <&-

Close standard input.

cmd <&n-

Move input file descriptor n instead of duplicating it. ksh93 only.

cmd >&n-

Move output file descriptor n instead of duplicating it. ksh93 only.

4.2.5.3. Multiple redirection

cmd 2>file

Send standard error to file; standard output remains the same (e.g., the screen).

cmd > file 2>&1

Send both standard error and standard output to file.

cmd > f1 2>f2

Send standard output to file f1, standard error to file f2.

cmd | tee files

Send output of cmd to standard output (usually the terminal) and to files. (See the Example in Chapter 2, under tee.)

cmd 2>&1 | tee files

Send standard output and error output of cmd to standard output (usually the terminal) and to files.

No space should appear between file descriptors and a redirection symbol; spacing is optional in the other cases.

4.2.5.4. Examples

$ cat part1 > book
$ cat part2 part3 >> book
$ mail tim < report
$ sed 's/^/XX /g' << END_ARCHIVE
> This is often how a shell archive is "wrapped",
> bundling text for distribution.  You would normally
> run sed from a shell program, not from the command line.
> END_ARCHIVE
XX This is often how a shell archive is "wrapped",
XX bundling text for distribution.  You would normally
XX run sed from a shell program, not from the command line.

To redirect standard output to standard error:

$ echo "Usage error:  see administrator" 1>&2

The following command sends output (files found) to filelist and error messages (inaccessible files) to file no_access:

$ find / -print > filelist 2>no_access


Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.