This section describes the many symbols peculiar to the Bourne and Korn shells. The topics are arranged as follows:
- /etc/profile
Executed automatically at login, first.
- $HOME/.profile
Executed automatically at login, second.
- $ENV
Specifies the name of a file to read when a new Korn shell is created. (ksh88
: all shells. ksh93
: interactive shells only.) The value is variable (ksh93
: and command and arithmetic) substituted in order to determine the actual file name. Login shells read $ENV after processing /etc/profile
and $HOME/.profile
.
- /etc/passwd
Source of home directories for ~
name
abbreviations. (On networked systems, this information may come from NIS or NIS+, not your workstation password file.)
In the Korn shell:
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.
$ 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.
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:
$ 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
$ 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
The usual input source or output destination can be changed, as seen in the following sections.
- cmd
>
file
Send output of cmd
to file
(overwrite).
- cmd
>>
file
Send output of cmd
to file
(append).
- cmd
<
file
Take input for cmd
from file
.
- cmd
<<
text
The contents of the shell script up to a line identical to text
become the standard input for cmd
(text
can be stored in a shell variable). This command form is sometimes called a Here document
. Input is usually typed at the keyboard or in the shell program. Commands that typically use this syntax include cat
, ex
, and sed
. (If <<-
is used, leading tabs are ignored when comparing input with the end-of-input text
marker.) If text
is quoted, the input is passed through verbatim. Otherwise, the contents are processed for variable and command substitutions. The Korn shell also processes the input for arithmetic substitution.
- cmd
<>
file
Korn shell only.
Open file
for reading and
writing on the standard input. The contents are not destroyed.[
]
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.
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.
|