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)
*  | 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.  | 
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.
| Class | Characters Matched | 
|---|---|
alnum  | |
alpha  | |
blank  | |
cntrl  | |
digit  | |
graph  | |
lower  | |
space  | |
upper  | |
xdigit  | 
$ ls new* List new and new.1 $ cat ch? Match ch9 but not ch10 $ vi [D-R]* Match files that begin with uppercase D through R $ pr !(*.o|core) | lp Korn shell only; print files that are not object files or core dumps
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.
| Character | Meaning | 
|---|---|
;  | 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:
| Sequence | Value | Sequence | Value | 
|---|---|---|---|
| \a | Alert | \nnn | Octal value nnn | 
| \b | Backspace | \xnn | Hexadecimal value nn | 
| \f | Form feed | \' | Single quote | 
| \n | Newline | \" | Double quote | 
| \r | Carriage return | \\ | Backslash | 
| \t | Tab | \E | Escape | 
| \v | Vertical tab | 
$ echo 'Single quotes "protect" double quotes' Single quotes "protect" double quotes $ echo "Well, isn't that \"special\"?" Well, isn't that "special"? $ echo "You have `ls | wc -l` files in `pwd`" You have 43 files in /home/bob $ echo "The value of \$x is $x" The value of $x is 100
cmd &  | Execute cmd in background.  | 
cmd1 ; cmd2  | Command sequence; execute multiple cmds on the same line.  | 
{ cmd1 ; cmd2 ; }  | Execute commands as a group in the current shell.  | 
(cmd1 ; cmd2)  | Execute commands as a group in a subshell.  | 
cmd1 | cmd2  | Pipe; use output from cmd1 as input to cmd2.  | 
cmd1 ‘cmd2‘  | Command substitution; use cmd2 output as arguments to cmd1.  | 
cmd1 $(cmd2)  | Korn shell command substitution; nesting is allowed.  | 
cmd $((expression))  | Korn shell arithmetic substitution. Use the result of expression as argument to cmd.  | 
cmd1 && cmd2  | AND; execute cmd1 and then (if cmd1 succeeds) cmd2. This is a “short-circuit” operation; cmd2 is never executed if cmd1 fails.  | 
cmd1 || cmd2  | OR; execute either cmd1 or (if cmd1 fails) cmd2. This is a “short-circuit” operation; cmd2 is never executed if cmd1 succeeds.  | 
$ nroff file > file.txt & Format in the background $ cd; ls Execute sequentially $ (date; who; pwd) > logfile All output is redirected $ sort file | pr -3 | lp Sort file, page output, then print $ vi `grep -l ifdef *.c` Edit files found by grep $ egrep '(yes|no)' `cat list` Specify a list of files to search $ egrep '(yes|no)' $(cat list) Korn shell version of previous $ egrep '(yes|no)' $(<list) Same, but faster $ grep XX file && lp file Print file if it contains the pattern; $ grep XX file || echo "XX not found" otherwise, echo an error message
| File Descriptor | Name | Common Abbreviation | Typical 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.
[5]With <, the file is opened read-only, and writes on the file descriptor will fail. With <>, the file is opened read-write; it is up to the application to actually take advantage of this.
cmd >&n  | Send cmd output to file descriptor 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  | |
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.  | 
cmd 2>file  | Send standard error to file; standard output remains the same (e.g., the screen).  | 
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.
$ 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
Coprocesses are a feature of the Korn shell only.
cmd1 | cmd2 |&  | Coprocess; execute the pipeline in the background. The shell sets up a two-way pipe, allowing redirection of both standard input and standard output.  | 
read -p var  | Read coprocess output into variable var.  | 
print -p string  | Write string to the coprocess.  | 
cmd <&p  | Take input for cmd from the coprocess.  | 
cmd >&p  | Send output of cmd to the coprocess.  | 
exec n<&p  | Move input for coprocess to file descriptor n.  | 
exec n>&p  | Move output from coprocess to file descriptor n.  | 
Moving the coprocess input and output file descriptors to standard file descriptors allows you to open multiple coprocesses.
$ ed - memo |& Start coprocess $ print -p /word/ Send ed command to coprocess $ read -p search Read output of ed command into variable search $ print "$search" Show the line on standard output A word to the wise.
Copyright © 2003 O'Reilly & Associates. All rights reserved.