1BSD/man1/sh.1

Compare this file to the similar file:
Show the results in this format:

.th SH I 10/28/76
.sh NAME
sh  \*-  shell (command interpreter)
.sh SYNOPSIS
.bd sh
[
.bd \*-tciv
]
[ name [ arg1 ... [ arg9 ... ] ] ]
.sh DESCRIPTION
.it Sh
is the standard command interpreter.
It is the program which reads and arranges the execution of
the command lines typed by most users.
It may itself be called as a command to interpret
files of commands.
Before discussing the arguments to the Shell
used as a command, the structure of command
lines themselves will be given.
.s3
.bd "Commands."
Each command is a sequence of non-blank command arguments
separated by blanks.
The
first argument specifies the name of a command to be
executed.
Except for certain types of special
arguments discussed below, the arguments
other than the command name are passed
without interpretation to the invoked
command.
.s3
If the first argument is the name of an executable
file, it is invoked;
If the first argument is not the name of an executable file,
and it does not contain the character `/', the shell will search
other directories for the command.  First it tries each user-specified library
(see lib (I)).  If it still has not found the command,
tries the standard system library `/bin'.
(In this way most of the standard UNIX commands,
which reside in `/bin', are found.)
If this fails, it finally tries
the library `/usr/bin',
where lesser-used UNIX commands live.
.s3
If a non-directory file has executable mode,
but not the form of an executable program
(does not begin with the proper magic number)
then it is examined to see if it is a Pascal object
(first word 0404 - a magic number).  If it is, then
a new pascal interpreter is created to interpret it.
Otherwise, it is assumed to be an ASCII file of commands
and a new Shell is created to execute it.
See ``Argument passing'' below.
.s3
If the file cannot be found,
a diagnostic is printed.
.s3
.bd "Command lines."
One or more commands separated by `|' or `^' constitute a
chain of
.it filters.
The standard output of each command but the last
is taken
as the standard input of the next command.
Each command is run as a separate process, connected
by pipes (see pipe(II)) to its neighbors.
A command line contained in
parentheses `( )' may appear in place of a simple command
as a filter.
.s3
A
.it "command line"
consists of one or
more pipelines separated, and perhaps terminated by `\fB;\fR' or `&'.
The semicolon designates sequential execution.
The `&' causes the preceding command line to be executed
without waiting for it to finish.
The process id of such a command line is reported, so that
it may be used if necessary for a subsequent
.it wait
or
.it kill.
If, however, the `&' is followed by a `*', i.e. `&*', then
the preceding set of `;' separated commands is
put in the background, as a group.  Thus `cc sh.c ; mv a.out sh &*' is equivalent
to `(cc sh.c ; mv a.out sh)&'.
.s3
.bd "Termination Reporting."
If a command (not followed by `&') terminates abnormally,
a message is printed.
(All terminations other than exit and interrupt
are considered abnormal.)
Termination reports for commands followed by `&'
are given upon receipt of the first
command subsequent to the termination of
the command,
or when a
.it wait
is executed.
The following is a list of the abnormal
termination messages:
.s3
.nf
	Bus error
	Trace/BPT trap
	Illegal instruction
	IOT trap
	EMT trap
	Bad system call
	Quit
	Floating exception
	Memory fault
	Killed
.s3
.fi
If a core image is produced,
`\*- Core dumped' is appended to the appropriate message.
.s3
.bd "Redirection of I/O."
There are three character sequences that cause the immediately following string
to be interpreted as a special argument to the Shell itself.
Such an argument may
appear anywhere among
the arguments of a simple command, or before or after
a parenthesized command list, and is associated with that
command or command list.
.s3
An argument of the form `<file' causes the file
`file'
to be used as the standard input (file descriptor 0) of the associated command.
.s3
An argument of the form `>file' causes file `file' to be used
as the standard output (file descriptor 1) for the associated command.
`File' is created if it did not exist, and in any case is truncated
at the outset.
.s3
An argument of the form `>>file' causes file `file' to be used as the
standard output for the associated command.
If `file'
did not exist, it is created; if it did exist,
the command output is appended to the file.
.s3
For example, either of the command lines
.s3
	ls >junk; cat tail >>junk
.br
	( ls; cat tail ) >junk
.s3
creates, on file `junk', a listing of the working directory, followed immediately
by the contents of file `tail'.
.s3
Either of the constructs `>file' or `>>file'
associated with any but the last command of a pipeline
is ineffectual, as is `<file' in any but the first.
.s3
In commands called by the Shell,
file descriptor 2 refers to the standard output of the
Shell before any redirection.
Thus filters may write diagnostics
to a location
where they have a chance to be seen.
If is not desired that they be seen, the forms `>* file',
`>>* file', and `|* ...' cause both units 1 and 2 to refer
to the specified file or put through the pipe.
.s3
.bd "Generation of argument lists."
If any argument contains any of the characters `?',
`*' or `[', it is treated specially as follows.
The current directory is searched for files which
.it match
the given argument.
.s3
The character `*' in an argument matches any string of characters
in a file name (including the null string).
.s3
The character `?' matches any
single character in a file name.
.s3
Square brackets `[...]' specify
a class of characters which
matches any single file-name character in the class.
Within the brackets,
each ordinary character is taken
to be a member of the class.
A pair of characters separated by `\*-' places
in the class
each character lexically greater than or equal to
the first and less than or equal to the second
member of the pair.
.s3
Other characters match only the same character in
the file name.
.s3
For example, `*' matches all file names;
`?' matches all one-character file names; `[ab]*.s' matches
all file names beginning with `a' or `b' and ending with `.s';
`?[zi\*-m]' matches all two-character file names ending
with `z' or the letters `i' through `m'.
.s3
If the argument with `*', `?', or `[' also contains a `/', a slightly
different procedure is used:  instead of the current directory,
the directory used is the one obtained
by taking the argument up to the last `/' before a `*' or `?'.
If a `/' appears thereafter, then the intervening string is matched
against the subdirectories of the derived directory.  If there is no
following `/',
the matching process matches the remainder of the argument
after this `/' against the files in the derived directory.
For example: `/usr/dmr/a*.s' matches
all files in directory `/usr/dmr' which begin
with `a' and end with `.s';
`/mnt*/*/.q' matches e.g. `/mnt/ken/.q' and `/mnt/chuck/.q' among
others.
.s3
In any event, a list of names is obtained which match
the argument.
This list is sorted into alphabetical order,
and the resulting sequence of arguments replaces the
single argument containing the `*', `[', or `?'.
The same process is carried out for each argument
(the resulting lists are
.it not
merged)
and finally the command is called with the resulting list of
arguments.
.s3
.bd "Quoting."
The character `\\' causes the immediately following character
to lose any special meaning it may have to the Shell;  in this
way `<', `>', and other characters meaningful to the
Shell may be passed as part of arguments.
A special case of this feature allows the continuation of commands
onto more than one line:  a new-line preceded by `\\' is translated
into a blank.
.s3
Sequences of characters enclosed in double (") or single (\*a)
quotes are also taken literally.
For example:
.s3
	ls  |  pr \*-h "My directory"
.s3
causes a directory listing to be produced
by
.it ls,
and passed on to
.it pr
to be
printed with the heading `My directory'.
Quotes permit the inclusion of blanks in
the heading, which is a single argument to
.it pr.
.s3
.bd "Argument passing."
When the Shell is invoked as a command, it has additional
string processing capabilities.
Recall that the form in which the Shell is invoked is
.s3
	sh [ name [ arg1 ... [ arg9 ... ] ] ]
.s3
The
.it name
is the name of a file which is read and
interpreted.
If not given, this subinstance of the Shell
continues to read the standard input file.
.s3
In command lines in the file
(not in command input),
character sequences of the form `$n', where
.it n
is a digit,
are replaced by the
\fIn\fRth argument to the invocation
of the Shell (argn).
`$0' is replaced by
.it name.
Finally `$*' is replaced by the remaining arguments
starting with `$1'.
.s3
The argument `\*-t,' causes
.it sh
to read the standard input for a single line, execute
it as a command, and then exit.
This facility replaces the older `mini-shell.'
It is useful for interactive programs
which allow users to execute
system commands.
.s3
The argument `\*-c' (used with one following argument)
causes the next argument to be taken as a command
line and executed.
No new-line need be present, but new-line characters
are treated appropriately.
This facility is useful as an alternative to
`-t' where the caller has already read
some of the characters of the command to be executed.
.s3
The argument `\*-i' causes the shell to be `interactive',
that is, the shell ignores interrupts and quits.
Login shells are interactive, as are
all shells whose input and output are both ttys.
Interactive shells also have interruptible waits (see wait(I)).
.s2
The argument `-v' causes the shell to echo each command line, with
a prompt, as it is printed.
.s3
.bd "End of file."
An end-of-file in the Shell's input causes it to exit.
A side effect of this fact means that the way to
log out from UNIX is to type an EOT.
.s3
.bd "Special commands."
The following commands are treated specially by the Shell.
.s3
.it chdir
is done without
spawning a new process by executing
.it "sys chdir"
(II).
"cd" is a useful abbreviation for "chdir".
.s3
.it login
is done by executing
/bin/login without creating a new process.
.s3
.it wait
is done without spawning a new process by
executing
.it "sys wait"
(II).
.s3
.it shift
is done by manipulating the arguments
to the Shell.
.s3
.it lib
is done by manipulating internal data.
.s3
`\fB:\fR' is simply ignored.
.s3
.bd "Command file errors; interrupts."
Any Shell-detected error, or an interrupt signal,
during the execution of a command file
causes the Shell to cease execution of that file.
.s3
Processes that are created with `&' ignore interrupts.
Also if such a process has not redirected its
input with a `<',
its input is automatically redirected to the
zero length file /dev/null.
.sh FILES
/etc/glob2,
which interprets `*', `?', and `['.
.br
/dev/null as a source of end-of-file.
.br
/bin/px	the Pascal interpreter.
.sh "SEE ALSO"
`The UNIX Time-Sharing System',
CACM, July, 1974.
.s2
chdir (I), login (I), wait (I), shift (I), glob (VIII), cd (I), lib (I)