This subsection describes the many symbols peculiar to the Bourne and Korn shell. The topics are arranged as follows:
Special files
Filename metacharacters
Quoting
Command forms
Redirection forms
Coprocesses (Korn shell only)
/etc/profile | Executed automatically at login. |
$HOME/.profile | Executed automatically at login. |
/etc/passwd | Source of home directories for ~ |
$ENV | Specifies the name of a file to read when a new Korn shell is created. |
* | Match any string of zero or more characters. |
? | Match any single character. |
[ | Match any one of the enclosed characters; a hyphen can be used to specify a range (e.g., a-z, A-Z, 0-9). |
[! | Match any character |
In the Korn shell:
?( | Match zero or one instance of |
*( | Match zero or more instances of |
+( | Match one or more instance of |
@( | Match exactly one instance of |
!( | Match any strings that don't contain |
~ | HOME directory of the current user. |
~ | HOME directory of user |
~+ | Current working directory (PWD). |
~- | Previous working directory (OLDPWD). |
The pattern
above can be a sequence of patterns separated by |,
meaning that the match applies to any of the patterns.
This extended syntax resembles that available to
egrep and awk.
$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. $cp !(Junk*|Temp*)*.c ..
Korn shell only. Copy C source files except forJunk
andTemp
files.
Quoting disables a character's special meaning and allows it to be used literally, as itself. The following characters have special meaning to the Bourne and Korn shells:
; | Command separator. |
& | Background execution. |
( ) | Command grouping. |
| | Pipe. |
> < & | Redirection symbols. |
* ? [ ] ~ + - @ ! | Filename metacharacters. |
" ' \ | Used in quoting other characters. |
` | Command substitution. |
$ | Variable substitution (or command substitution). |
newline space tab | Word separators. |
The characters below can be used for quoting:
Everything between " and " is taken literally, except for the following characters that keep their special meaning:
Variable substitution will occur.
Command substitution will occur.
This marks the end of the double quote.
' '
Everything between '
and '
is taken literally except for another '
.
\
The character following a \ is taken literally. Use within " " to escape ", $, and `. Often used to escape itself, spaces, or newlines.
$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
| Execute |
| Command sequence; execute multiple |
( | Subshell; treat |
| Pipe; use output from |
| Command substitution; use |
| Korn-shell command substitution; nesting is allowed. |
| AND; execute |
| OR; execute either |
{ | Execute commands in the current shell. |
$nroff file &
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 | Common | Typical | |
---|---|---|---|
Descriptor | Name | Abbreviation | 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 follows:
| Send output of |
| Send output of |
| Take input for |
| Read standard input up to a line identical to |
| Send |
| Same, except that output that would normally go to file descriptor |
| Close standard output. |
| Take input for |
| Same, except that input that would normally come from file descriptor |
| Close standard input. |
| Send standard error to |
| Send both standard error and standard output to |
( | Send standard output to file |
| Send output of |
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 will send output (files found) to filelist and send error messages (inaccessible files) to file no_access:
$(find / -print > filelist) 2>no_access
Coprocesses are a feature of the Korn shell only.
| 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 | Read coprocess input into variable |
print -p | Write |
| Take input for |
| Send output of |
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.