BSD 1 development
[unix-history] / man1 / sh.1
CommitLineData
fde98a2d
BJ
1.th SH I 10/28/76
2.sh NAME
3sh \*- shell (command interpreter)
4.sh SYNOPSIS
5.bd sh
6[
7.bd \*-tciv
8]
9[ name [ arg1 ... [ arg9 ... ] ] ]
10.sh DESCRIPTION
11.it Sh
12is the standard command interpreter.
13It is the program which reads and arranges the execution of
14the command lines typed by most users.
15It may itself be called as a command to interpret
16files of commands.
17Before discussing the arguments to the Shell
18used as a command, the structure of command
19lines themselves will be given.
20.s3
21.bd "Commands."
22Each command is a sequence of non-blank command arguments
23separated by blanks.
24The
25first argument specifies the name of a command to be
26executed.
27Except for certain types of special
28arguments discussed below, the arguments
29other than the command name are passed
30without interpretation to the invoked
31command.
32.s3
33If the first argument is the name of an executable
34file, it is invoked;
35If the first argument is not the name of an executable file,
36and it does not contain the character `/', the shell will search
37other directories for the command. First it tries each user-specified library
38(see lib (I)). If it still has not found the command,
39tries the standard system library `/bin'.
40(In this way most of the standard UNIX commands,
41which reside in `/bin', are found.)
42If this fails, it finally tries
43the library `/usr/bin',
44where lesser-used UNIX commands live.
45.s3
46If a non-directory file has executable mode,
47but not the form of an executable program
48(does not begin with the proper magic number)
49then it is examined to see if it is a Pascal object
50(first word 0404 - a magic number). If it is, then
51a new pascal interpreter is created to interpret it.
52Otherwise, it is assumed to be an ASCII file of commands
53and a new Shell is created to execute it.
54See ``Argument passing'' below.
55.s3
56If the file cannot be found,
57a diagnostic is printed.
58.s3
59.bd "Command lines."
60One or more commands separated by `|' or `^' constitute a
61chain of
62.it filters.
63The standard output of each command but the last
64is taken
65as the standard input of the next command.
66Each command is run as a separate process, connected
67by pipes (see pipe(II)) to its neighbors.
68A command line contained in
69parentheses `( )' may appear in place of a simple command
70as a filter.
71.s3
72A
73.it "command line"
74consists of one or
75more pipelines separated, and perhaps terminated by `\fB;\fR' or `&'.
76The semicolon designates sequential execution.
77The `&' causes the preceding command line to be executed
78without waiting for it to finish.
79The process id of such a command line is reported, so that
80it may be used if necessary for a subsequent
81.it wait
82or
83.it kill.
84If, however, the `&' is followed by a `*', i.e. `&*', then
85the preceding set of `;' separated commands is
86put in the background, as a group. Thus `cc sh.c ; mv a.out sh &*' is equivalent
87to `(cc sh.c ; mv a.out sh)&'.
88.s3
89.bd "Termination Reporting."
90If a command (not followed by `&') terminates abnormally,
91a message is printed.
92(All terminations other than exit and interrupt
93are considered abnormal.)
94Termination reports for commands followed by `&'
95are given upon receipt of the first
96command subsequent to the termination of
97the command,
98or when a
99.it wait
100is executed.
101The following is a list of the abnormal
102termination messages:
103.s3
104.nf
105 Bus error
106 Trace/BPT trap
107 Illegal instruction
108 IOT trap
109 EMT trap
110 Bad system call
111 Quit
112 Floating exception
113 Memory fault
114 Killed
115.s3
116.fi
117If a core image is produced,
118`\*- Core dumped' is appended to the appropriate message.
119.s3
120.bd "Redirection of I/O."
121There are three character sequences that cause the immediately following string
122to be interpreted as a special argument to the Shell itself.
123Such an argument may
124appear anywhere among
125the arguments of a simple command, or before or after
126a parenthesized command list, and is associated with that
127command or command list.
128.s3
129An argument of the form `<file' causes the file
130`file'
131to be used as the standard input (file descriptor 0) of the associated command.
132.s3
133An argument of the form `>file' causes file `file' to be used
134as the standard output (file descriptor 1) for the associated command.
135`File' is created if it did not exist, and in any case is truncated
136at the outset.
137.s3
138An argument of the form `>>file' causes file `file' to be used as the
139standard output for the associated command.
140If `file'
141did not exist, it is created; if it did exist,
142the command output is appended to the file.
143.s3
144For example, either of the command lines
145.s3
146 ls >junk; cat tail >>junk
147.br
148 ( ls; cat tail ) >junk
149.s3
150creates, on file `junk', a listing of the working directory, followed immediately
151by the contents of file `tail'.
152.s3
153Either of the constructs `>file' or `>>file'
154associated with any but the last command of a pipeline
155is ineffectual, as is `<file' in any but the first.
156.s3
157In commands called by the Shell,
158file descriptor 2 refers to the standard output of the
159Shell before any redirection.
160Thus filters may write diagnostics
161to a location
162where they have a chance to be seen.
163If is not desired that they be seen, the forms `>* file',
164`>>* file', and `|* ...' cause both units 1 and 2 to refer
165to the specified file or put through the pipe.
166.s3
167.bd "Generation of argument lists."
168If any argument contains any of the characters `?',
169`*' or `[', it is treated specially as follows.
170The current directory is searched for files which
171.it match
172the given argument.
173.s3
174The character `*' in an argument matches any string of characters
175in a file name (including the null string).
176.s3
177The character `?' matches any
178single character in a file name.
179.s3
180Square brackets `[...]' specify
181a class of characters which
182matches any single file-name character in the class.
183Within the brackets,
184each ordinary character is taken
185to be a member of the class.
186A pair of characters separated by `\*-' places
187in the class
188each character lexically greater than or equal to
189the first and less than or equal to the second
190member of the pair.
191.s3
192Other characters match only the same character in
193the file name.
194.s3
195For example, `*' matches all file names;
196`?' matches all one-character file names; `[ab]*.s' matches
197all file names beginning with `a' or `b' and ending with `.s';
198`?[zi\*-m]' matches all two-character file names ending
199with `z' or the letters `i' through `m'.
200.s3
201If the argument with `*', `?', or `[' also contains a `/', a slightly
202different procedure is used: instead of the current directory,
203the directory used is the one obtained
204by taking the argument up to the last `/' before a `*' or `?'.
205If a `/' appears thereafter, then the intervening string is matched
206against the subdirectories of the derived directory. If there is no
207following `/',
208the matching process matches the remainder of the argument
209after this `/' against the files in the derived directory.
210For example: `/usr/dmr/a*.s' matches
211all files in directory `/usr/dmr' which begin
212with `a' and end with `.s';
213`/mnt*/*/.q' matches e.g. `/mnt/ken/.q' and `/mnt/chuck/.q' among
214others.
215.s3
216In any event, a list of names is obtained which match
217the argument.
218This list is sorted into alphabetical order,
219and the resulting sequence of arguments replaces the
220single argument containing the `*', `[', or `?'.
221The same process is carried out for each argument
222(the resulting lists are
223.it not
224merged)
225and finally the command is called with the resulting list of
226arguments.
227.s3
228.bd "Quoting."
229The character `\\' causes the immediately following character
230to lose any special meaning it may have to the Shell; in this
231way `<', `>', and other characters meaningful to the
232Shell may be passed as part of arguments.
233A special case of this feature allows the continuation of commands
234onto more than one line: a new-line preceded by `\\' is translated
235into a blank.
236.s3
237Sequences of characters enclosed in double (") or single (\*a)
238quotes are also taken literally.
239For example:
240.s3
241 ls | pr \*-h "My directory"
242.s3
243causes a directory listing to be produced
244by
245.it ls,
246and passed on to
247.it pr
248to be
249printed with the heading `My directory'.
250Quotes permit the inclusion of blanks in
251the heading, which is a single argument to
252.it pr.
253.s3
254.bd "Argument passing."
255When the Shell is invoked as a command, it has additional
256string processing capabilities.
257Recall that the form in which the Shell is invoked is
258.s3
259 sh [ name [ arg1 ... [ arg9 ... ] ] ]
260.s3
261The
262.it name
263is the name of a file which is read and
264interpreted.
265If not given, this subinstance of the Shell
266continues to read the standard input file.
267.s3
268In command lines in the file
269(not in command input),
270character sequences of the form `$n', where
271.it n
272is a digit,
273are replaced by the
274\fIn\fRth argument to the invocation
275of the Shell (argn).
276`$0' is replaced by
277.it name.
278Finally `$*' is replaced by the remaining arguments
279starting with `$1'.
280.s3
281The argument `\*-t,' causes
282.it sh
283to read the standard input for a single line, execute
284it as a command, and then exit.
285This facility replaces the older `mini-shell.'
286It is useful for interactive programs
287which allow users to execute
288system commands.
289.s3
290The argument `\*-c' (used with one following argument)
291causes the next argument to be taken as a command
292line and executed.
293No new-line need be present, but new-line characters
294are treated appropriately.
295This facility is useful as an alternative to
296`-t' where the caller has already read
297some of the characters of the command to be executed.
298.s3
299The argument `\*-i' causes the shell to be `interactive',
300that is, the shell ignores interrupts and quits.
301Login shells are interactive, as are
302all shells whose input and output are both ttys.
303Interactive shells also have interruptible waits (see wait(I)).
304.s2
305The argument `-v' causes the shell to echo each command line, with
306a prompt, as it is printed.
307.s3
308.bd "End of file."
309An end-of-file in the Shell's input causes it to exit.
310A side effect of this fact means that the way to
311log out from UNIX is to type an EOT.
312.s3
313.bd "Special commands."
314The following commands are treated specially by the Shell.
315.s3
316.it chdir
317is done without
318spawning a new process by executing
319.it "sys chdir"
320(II).
321"cd" is a useful abbreviation for "chdir".
322.s3
323.it login
324is done by executing
325/bin/login without creating a new process.
326.s3
327.it wait
328is done without spawning a new process by
329executing
330.it "sys wait"
331(II).
332.s3
333.it shift
334is done by manipulating the arguments
335to the Shell.
336.s3
337.it lib
338is done by manipulating internal data.
339.s3
340`\fB:\fR' is simply ignored.
341.s3
342.bd "Command file errors; interrupts."
343Any Shell-detected error, or an interrupt signal,
344during the execution of a command file
345causes the Shell to cease execution of that file.
346.s3
347Processes that are created with `&' ignore interrupts.
348Also if such a process has not redirected its
349input with a `<',
350its input is automatically redirected to the
351zero length file /dev/null.
352.sh FILES
353/etc/glob2,
354which interprets `*', `?', and `['.
355.br
356/dev/null as a source of end-of-file.
357.br
358/bin/px the Pascal interpreter.
359.sh "SEE ALSO"
360`The UNIX Time-Sharing System',
361CACM, July, 1974.
362.s2
363chdir (I), login (I), wait (I), shift (I), glob (VIII), cd (I), lib (I)