.\" Man page portability notes
.\" These are some notes on conventions to maintain for greatest
.\" portability of this man page to various other versions of
.\" When you want a \ to appear in the output, use \e in the man page.
.\" (NOTE this comes up in the rc grammar, where to print out '\n' the
.\" man page must contain '\en'.)
.\" Evidently not all versions of nroff allow the omission of the
.\" terminal " on a macro argument. Thus what could be written
.\" .Cr "exec >[2] err.out
.\" in true nroffs must be written
.\" .Cr "exec >[2] err.out"
.\" Use symbolic font names (e.g. R, I, B) instead of the standard
.\" font positions 1, 2, 3. Note that for Xf to work the standard
.\" font names must be single characters.
.\" Not all man macros have the RS and RE requests (I altered the Ds
.\" and De macros and the calls to Ds accordingly).
.\" Thanks to Michael Haardt (u31b3hs@cip-s01.informatik.rwth-aachen.de)
.\" for pointing out these problems.
.\" Note that sentences should end at the end of a line. nroff and
.\" troff will supply the correct intersentence spacing, but only if
.\" the sentences end at the end of a line. Explicit spaces, if given,
.\" are apparently honored and the normal intersentence spacing is
.\" Dd distance to space vertically before a "display"
.\" These are what n/troff use for interparagraph distance
.\" Ds begin a display, indented .5 inches from the surrounding text.
.\" Note that uses of Ds and De may NOT be nested.
.\" De end a display (no trailing vertical spacing)
.\" I stole the Xf macro from the -man macros on my machine (originally
.\" "}S", I renamed it so that it won't conflict).
.\" Set Cf to the name of the constant width font.
.\" It will be "C" or "(CW", typically.
.\" NOTEZ BIEN the lines defining Cf must have no trailing white space:
.\" Rc - Alternate Roman and Courier
.Xf R \\*(Cf \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
.\" Ic - Alternate Italic and Courier
.Xf I \\*(Cf \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
.\" Bc - Alternate Bold and Courier
.Xf B \\*(Cf \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
.\" Cr - Alternate Courier and Roman
.Xf \\*(Cf R \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
.\" Ci - Alternate Courier and Italic
.Xf \\*(Cf I \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
.\" Cb - Alternate Courier and Bold
.Xf \\*(Cf B \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
.\" \$3 - desired word with embedded font changes, built up by recursion
.\" \$4 - text for first font
.\" \$5 - \$9 - remaining args
.\" Every time we are called:
.\" If there is something in \$4
.\" then Call ourself with the fonts switched,
.\" with a new word made of the current word (\$3) and \$4
.\" rendered in the first font,
.\" and with the remaining args following \$4.
.\" else We are done recursing. \$3 holds the desired output
.\" word. We emit \$3, change to Roman font, and restore
.\" the point size to the default.
.\" Use Xi to add a little bit of space after italic text.
.\" I used to test for the italic font both by its font position
.\" and its name. Now just test by its name.
.\" .if "\\$1"2" .if !"\\$5"" .ds Xi \^
.if "\\$1"I" .if !"\\$5"" .ds Xi \^
.\" This is my original code to deal with the recursion.
.\" Evidently some nroffs can't deal with it.
.\" . Xf \\$2 \\$1 "\\$3\\f\\$1\\$4\\*(Xi" "\\$5" "\\$6" "\\$7" "\\$8" "\\$9"
.\" . ft R \" Restore the default font, since we don't know
.\" . \" what the last font change was.
.\" . ps 10 \" Restore the default point size, since it might
.\" . \" have been changed by an argument to this macro.
.\" Here is more portable (though less pretty) code to deal with
.if !"\\$4"" .Xf \\$2 \\$1 "\\$3\\f\\$1\\$4\\*(Xi" "\\$5" "\\$6" "\\$7" "\\$8" "\\$9"
is a command interpreter and programming language similar to
It is based on the AT&T Plan 9 shell of the same name.
The shell offers a C-like syntax (much more so than the C shell),
and a powerful mechanism for manipulating variables.
It is reasonably small and reasonably fast,
especially when compared to contemporary shells.
Its use is intended to be interactive,
but the language lends itself well to scripts.
will exit if the exit status of a command is false (nonzero).
will not exit, however, if a conditional fails, e.g., an
option is present or if the input to
is from a terminal (as determined by
will be printed before an
print every command on standard error before it is executed.
It can be useful for debugging
This option will echo input to
on standard error as it is read.
will behave as a login shell.
That is, it will try to run commands present in
if this file exists, before reading any other input.
can be made to dump core if sent
This option is only useful for debugging
to read its input and parse it, but not to execute any commands.
This is useful for syntax checking on scripts.
If used in combination with the
will print each command as it is parsed in a form similar to the one
used for exporting functions into the environment.
from initializing shell functions from the environment.
to run in a protected mode, whereby it becomes more difficult for
script to be subverted by placing false commands in the environment.
(Note that this presence of this option does NOT mean that it is safe to
scripts; the usual caveats about the setuid bit still apply.)
This flag prevents the usual practice of trying to open
on file descriptors 0, 1, and 2, if any of those descriptors
is present, commands are executed from the immediately following
A simple command is a sequence of words, separated by white space
(space and tab) characters that ends with a newline, semicolon
The first word of a command is the name of that command.
then the name is used as an absolute path
name referring to an executable file.
Otherwise, the name of the command is looked up in a table
of shell functions, builtin commands,
or as a file in the directories named by
is run in the background; that is,
the shell returns immediately rather than waiting for the command to
connected to their standard input unless an explicit redirection for
A command prefixed with an at-sign
is executed in a subshell.
This insulates the parent shell from the effects
of state changing operations such as a
or a variable assignment.
but leaves the shell running in the current directory.
A long logical line may be continued over several physical lines by
terminating each line (except the last) with a backslash
The backslash-newline sequence is treated as a space.
A backslash is not otherwise special to
inside quotes a backslash loses its special meaning
even when it is followed by a newline.)
interprets several characters specially; special characters
automatically terminate words.
The following characters are special:
.Cr "# ; & | ^ $ = \` ' { } ( ) < >"
prevents special treatment of any character other than itself.
All characters, including control characters, newlines,
and backslashes between two quote characters are treated as an
A quote character itself may be quoted by placing two quotes in a row.
The minimal sequence needed to enter the quote character is
The empty string is represented by
.Cr "echo 'What''s the plan, Stan?'"
.Cr "What's the plan, Stan?"
All characters up to but not including the next newline are ignored.
Note that backslash continuation does not work inside a comment,
the backslash is ignored along with everything else.
Zero or more commands may be grouped within braces
and are then treated as one command.
Braces do not otherwise define scope;
they are used only for command grouping.
In particular, be wary of the command:
Since pipe binds tighter than
this command does not perform what the user expects it to.
Instead, enclose the whole
.Cr "{for (i) command} | command"
grammar is simple enough that a (confident) user can
understand it by examining the skeletal
at the end of this man page (see the section entitled
The standard output may be redirected to a file with
and the standard input may be taken from a file with
File descriptors other than 0 and 1 may be specified also.
For example, to redirect standard error to a file, use:
In order to duplicate a file descriptor, use
Thus to redirect both standard output and standard error
.Cr "command > file >[2=1]"
To close a file descriptor that may be open, use
In order to place the output of a command at the end of an already
If the file does not exist, then it is created.
``Here documents'' are supported as in
.Cr "command << 'eof-marker'"
If the end-of-file marker is enclosed in quotes,
then no variable substitution occurs inside the here document.
Otherwise, every variable is substituted
by its space-separated-list value (see
character follows a variable name, it is deleted.
This allows the unambiguous use of variables adjacent to text, as in
in a here document when an unquoted end-of-file marker is being used,
supports ``here strings'', which are like here documents,
except that input is taken directly from a string on the command line.
Its use is illustrated here:
.Cr "cat <<< 'this is a here string' | wc"
to export functions using here documents into the environment;
the author does not expect users to find this feature useful.)
Two or more commands may be combined in a pipeline by placing the
The standard output (file descriptor 1)
of the command on the left is tied to the standard input (file
descriptor 0) of the command on the right.
indicates that file descriptor
of the left process is connected to
As an example, to pipe the standard error of a command to
The exit status of a pipeline is considered true if and only if every
command in the pipeline exits true.
.SS "Commands as Arguments"
take their arguments on the command
line, and do not read input from standard input.
sometimes to build nonlinear pipelines so that a command like
can read the output of two other commands at once.
.Cr "cmp <{command} <{command}"
compares the output of the two commands in braces.
A note: since this form of
redirection is implemented with some kind of pipe, and since one cannot
on a pipe, commands that use
Data can be sent down a pipe to several commands using
and the output version of this notation:
.Cr "echo hi there | tee >{sed 's/^/p1 /'} >{sed 's/^/p2 /'}"
The following may be used for control flow in
is executed, and if its return status is zero, the first
command is executed, otherwise the second is.
Braces are not mandatory around the commands.
statement is valid only if it
follows a close-brace on the same line.
is taken to be a simple-if:
.SS "While and For Loops"
.Ci "while (" test ) " cmd"
and performs the command as long as the
.Ci "for (" var " in" " list" ) " cmd"
(which may contain variables and backquote substitutions) and runs
.Cr "for (i in \`{ls -F | grep '\e*$' | sed 's/\e*$//'}) { commands }"
to the name of each file in the current directory that is
.Ci "switch (" list ") { case" " ..." " }"
looks inside the braces after a
for statements beginning with the word
If any of the patterns following
match the list supplied to
then the commands up until the next
matching is performed only against the strings in
(Matching for case statements is the same as for the
There are a number of operators in
which depend on the exit status of a command.
executes the first command and then executes the second command if and only if
the first command exits with a zero exit status (``true'' in Unix).
executes the first command executing the second command if and only if
the second command exits with a nonzero exit status (``false'' in Unix).
negates the exit status of a command.
There are two forms of pattern matching in
One is traditional shell globbing.
This occurs in matching for file names in argument lists:
.Cr "command argument argument ..."
occur in an argument or command,
argument as a pattern for matching against files.
(Contrary to the behavior other shells exhibit,
will only perform pattern matching if a metacharacter occurs unquoted and
will always echo just a star.
In order for non-literal metacharacters to be expanded, an
statement must be used in order to rescan the input.)
Pattern matching occurs according to the following rules: a
matches any number (including zero) of
matches any single character, and a
number of characters followed by a
matches a single character in that
The rules for character class matching are the same as those for
with the exception that character class negation is achieved
since the caret already means
also matches patterns against strings with the
.Cr "~ subject pattern pattern ..."
to zero if and only if a supplied pattern matches any
single element of the subject list.
sets status to zero, while
The null list is matched by the null list, so
This may also be achieved
does not match patterns against file
names, so it is not necessary to quote the characters
does expand the glob the subject against filenames if it contains
returns true if any of the files in the current directory have a
command is given a list as its first
argument, then a successful match against any of the elements of that
.SH "LISTS AND VARIABLES"
The primary data structure in
is the list, which is a sequence of words.
Parentheses are used to group lists.
The empty list is represented by
Lists have no hierarchical structure;
a list inside another list is expanded so the
outer list contains all the elements of the inner list.
Thus, the following are all equivalent
.Cr "((one) () ((two three)))"
Note that the null string,
Assigning the null string to variable is a valid
operation, but it does not remove its definition.
Two lists may be joined by the concatenation operator
A single word is treated as a list of length one, so
For lists of more than one element,
concatenation works according to the following rules:
if the two lists have the same number of elements,
then concatenation is pairwise:
.Cr "echo (a\- b\- c\-)^(1 2 3)"
Otherwise, one of the lists must have a single element,
and then the concatenation is distributive:
.Cr "cc \-^(O g c) (malloc alloca)^.c"
has the effect of performing the command
.Cr "cc \-O \-g \-c malloc.c alloca.c"
inserts carets (concatenation operators) for free in certain situations,
in order to save some typing on the user's behalf.
example, the above example could also be typed in as:
.Cr "opts=(O g c) files=(malloc alloca) cc \-$opts $files.c"
takes care to insert a free-caret between the
The rule for free carets is as follows: if
a word or keyword is immediately
followed by another word, keyword, dollar-sign or
inserts a caret between them.
A list may be assigned to a variable, using the notation:
Any sequence of non-special characters, except a sequence including
only digits, may be used as a variable name.
All user-defined variables are exported into the environment.
The value of a variable is referenced with the notation:
Any variable which has not been assigned a value returns the null list,
In addition, multiple references are allowed:
A variable's definition may also be removed by
assigning the null list to a variable:
For ``free careting'' to work correctly,
must make certain assumptions
about what characters may appear in a variable name.
assumes that a variable name consists only of alphanumeric characters,
To reference a variable with other
characters in its name, quote the variable name.
.Cr "echo $'we$Ird\Variab!le'"
Any number of variable assignments may be made local to a single
.Cr "a=foo b=bar ... command"
The command may be a compound command, so for example:
for the duration of one long compound command.
.SS "Variable Subscripts"
Variables may be subscripted with the notation
is a list of integers (origin 1).
The list of subscripts need
not be in order or even unique.
references a nonexistent element, then
is an integer, is a shorthand for
arguments may be referred to as
Note also that the list of subscripts may be given by any of
.Cr "$var(\`{awk 'BEGIN{for(i=1;i<=10;i++)print i;exit; }'})"
returns the first 10 elements of
To count the number of elements in a variable, use
This returns a single-element list, with the number of elements in
In order to create a single-element list from a multi-element list,
with the components space-separated, use
This is useful when the normal list concatenation rules need to be
For example, to append a single period at the end of
.SS "Backquote Substitution"
A list may be formed from the output of a command by using backquote
returns a list formed from the standard output of the command in braces.
is used to split the output into list elements.
has the value space-tab-newline.
The braces may be omitted if the command is a single word.
This last feature is useful when defining functions that expand
to useful argument lists.
.Cr "fn src { echo *.[chy] }"
(This will print out a word-count of all C source files in the current
In order to override the value of
.Cr "\`\` (ifs-list) { command }"
will be temporarily ignored and the command's output will be split as specified by
the list following the double backquote.
.Cr "\`\` ($nl :) {cat /etc/passwd}"
into fields, assuming that
Several variables are known to
and are treated specially.
is set to the name of a function for the duration of
the execution of that function, and
is also set to the name of the
file being interpreted for the duration of a
The process ID of the last process started in the background.
The process IDs of any background processes which are outstanding
or which have died and have not been waited for yet.
A list of directories to search for the target of a
The empty string stands for the current directory.
variable does not contain the current directory, then the current
directory will not be searched; this allows directory searching to
begin in a directory other than the current directory.
Note also that an assignment to
causes an automatic assignment to
contains the name of a file to which commands are appended as
This facilitates the use of a stand-alone history program
which parses the contents of the history file and presents them to
does not append commands to any file.
The default directory for the builtin
command and is the directory
looks to find its initialization file,
has been started up as a login shell.
are aliased to each other.
The internal field separator, used for splitting up the output of
backquote commands for digestion as a list.
This is a list of directories to search in for commands.
The empty string stands for the current directory.
are aliased to each other.
is not set at startup time,
assumes a default value suitable for your system.
.Cr "(/usr/ucb /usr/bin /bin .)"
The process ID of the currently running
This variable holds the two prompts (in list form, of course) that
is printed before each command is read, and
is printed when input is expected to continue on the next
The reason for this is that it enables an
user to grab commands from previous lines using a
mouse, and to present them to
for re-interpretation; the semicolon
prompt is simply ignored by
script, when typed interactively, will not leave
and can therefore be grabbed by a mouse and placed
directly into a file for use as a shell script, without further editing
If this function is set, then it gets executed every time
The exit status of the last command.
If the command exited with a numeric value,
that number is the status.
If the died with a signal,
the status is the name of that signal; if a core file
for a pipeline is a list, with one entry,
as above, for each process in the pipeline.
are derived from the environment
Otherwise, they are derived from
the environment values of
This is for compatibility with other Unix programs, like
are assumed to be colon-separated lists.
functions are identical to
scripts, except that they are stored
in memory and are automatically exported into the environment.
A shell function is declared as:
.Cr "fn name { commands }"
scans the definition until the close-brace, so the function can
The function definition may be removed by typing
(One or more names may be specified.
With an accompanying definition, all names receive the same definition.
for assigning the same signal handler to many signals.
Without a definition, all named functions are deleted.)
When a function is executed,
is set to the arguments to that
function for the duration of the command.
Thus a reasonable definition for
.SH "INTERRUPTS AND SIGNALS"
recognizes a number of signals, and allows the user to define shell
functions which act as signal handlers.
when it is in interactive mode.
has been invoked with the
However, user-defined signal handlers may be written for these and
The way to define a signal handler is to
write a function by the name of the signal in lower case.
.Cr "fn sighup { echo hangup; rm /tmp/rc$pid.*; exit }"
In addition to Unix signals,
recognizes the artificial signal
In order to remove a signal handler's definition,
remove it as though it were a regular function.
In order to ignore a signal, set the signal handler's value to
causes SIGINT to be ignored by the shell.
Only signals that are being ignored are passed on to programs run by
signal functions are not exported.
On System V-based Unix systems,
will not allow you to trap
Builtin commands execute in the context of the shell, but otherwise
behave exactly like other commands.
are not strictly speaking builtin commands,
they can usually be used as such.
\&\fB.\fR [\fB\-i\fR] \fIfile \fR[\fIarg ...\fR]
and executes its contents.
flag, input is interactive.
Thus from within a shell script,
does the ``right'' thing.
Breaks from the innermost
keyword between commands in
\fBbuiltin \fIcommand \fR[\fIarg ...\fR]
Executes the command ignoring any function definition of the
This command is present to allow functions with the
same names as builtins to use the underlying builtin or binary.
\fBcd \fR[\fIdirectory\fR]
Changes the current directory to
is searched for possible locations of
analogous to the searching of
changes the current directory to
\fBecho \fR[\fB\-n\fR] [\fB\-\|\-\fR] [\fIarg ...\fR]
Prints its arguments to standard output, terminated by a newline.
Arguments are separated by spaces.
no final newline is printed.
then all other arguments are echoed literally.
This is used for echoing a literal
Concatenates the elements of
with spaces and feeds the resulting string to
This is the only time input is rescanned in
\fBexec \fR[\fIarg ...\fR]
If the exec contains only redirections,
then these redirections apply to the current shell
and the shell does not exit.
places further output to standard error in the file
\fBexit \fR[\fIstatus\fR]
Cause the current shell to exit with the given exit
If no argument is given, the current value of
\fBlimit \fR[\fB\-h\fR] [\fIresource \fR[\fIvalue\fR]]
builtin, this command operates upon the
BSD-style limits of a process.
flag displays/alters the hard
The resources which can be shown or altered are
.Cr "limit coredumpsize 0"
into a new process group.
This builtin is useful for making
behave like a job-control shell in a hostile environment.
One example is the NeXT Terminal program, which implicitly assumes
that each shell it forks will put itself into a new process group.
Returns from the current function, with status
is a single value or a list of possible exit statuses.
.Cr "return (sigpipe 1 2 3)"
(This is commonly used to allow a function to return with the exit status
of a previously executed pipeline of commands.)
when not inside a function.
elements from the beginning of
Sets the current umask (see
If no argument is present, the current mask value is printed.
which must have been started by
waits for any child process to exit.
\fBwhatis \fR[\fB\-s\fR] [\fB\-\|\-\fR] [\fIname ...\fR]
Prints a definition of the named objects.
For variables, their values
are printed; for functions, their definitions are; and for executable
files, path names are printed.
prints the values of all shell variables and functions.
also prints out a list of available signals and their handlers (if any).
output is suitable for input to
in a file, it should be possible to recreate the state of
by sourcing this file with a
cannot be used to store the state of
signal handlers in a file, because builtins with redirections
are run in a subshell, and
always restores signal handlers to their default value after a
to parse its arguments, you can use the special argument
to terminate its options.
This allows you to use names beginning with a dash, such as
grammar, edited to remove semantic actions.
%term ANDAND BACKBACK BANG CASE COUNT DUP ELSE END FLAT FN FOR IF IN
%term OROR PIPE REDIR SUB SUBSHELL SWITCH TWIDDLE WHILE WORD HUH
end: END /* EOF */ | '\en'
cmdsan: cmdsa | cmd '\en'
epilog: /* empty */ | redir epilog
case: CASE words ';' | CASE words '\en'
cbody: cmd | case cbody | cmdsan cbody
cmd : /* empty */ %prec WHILE
| FOR '(' word IN words ')' optnl cmd
| FOR '(' word ')' optnl cmd
| SWITCH '(' word ')' optnl '{' cbody '}'
| TWIDDLE optcaret word words
optcaret: /* empty */ | '^'
simple: first | simple word | simple redir
first: comword | first '^' sword
word: sword | word '^' sword
| '$' sword SUB words ')'
| BACKBACK word brace | BACKBACK word sword
keyword: FOR | IN | WHILE | IF | SWITCH
| FN | ELSE | CASE | TWIDDLE | BANG | SUBSHELL
words: /* empty */ | words word
optnl: /* empty */ | optnl '\en'
was written by Byron Rakitzis, with valuable help
from Paul Haahr, Hugh Redelmeier and David Sanderson.
The design of this shell has been copied from the
that Tom Duff wrote at Bell Labs.
style redirection is implemented that way.
However, on other systems it is implemented with named pipes,
into removing the FIFO it places in
prematurely, or it is even possible to cause
should be available for variables other than
is built in only for performance reasons, which is a bad idea.
There should be a way to avoid exporting a variable.
notation for flattening should allow for using an arbitrary
separating character, not just space.
Bug reports should be mailed to
.Cr "byron@archone.tamu.edu" .
Here is a list of features which distinguish this incarnation of
from the one described in the Bell Labs manual pages:
clause which gets executed
Backquotes are slightly different in v10
a backquote must always be followed by a left-brace.
This restriction is not present for single-word commands in this
The following are all new with this version of
the list flattening operator,
here strings (they facilitate exporting of functions
with here documents into the environment),
to the name of a function being executed/file
``rc \(em A Shell for Plan 9 and UNIX Systems'',
vol. 2. (Saunders College Publishing)
(This paper is also distributed with this