document distributed with 4.1BSD
[unix-history] / usr / src / bin / sh / USD.doc / t1
CommitLineData
a1168a85
KD
1.\" @(#)t1 4.1 (Berkeley) %G%
2.\"
3.RP
4.TL
5An Introduction to the UNIX Shell
6.AU
7S. R. Bourne
8.AI
9.MH
10.AB
11.LP
12The
13.ul
14shell
15is a command programming language that provides an interface
16to the
17.UX
18operating system.
19Its features include
20control-flow primitives, parameter passing, variables and
21string substitution.
22Constructs such as
23.ul
24while, if then else, case
25and
26.ul
27for
28are available.
29Two-way communication is possible between the
30.ul
31shell
32and commands.
33String-valued parameters, typically file names or flags, may be
34passed to a command.
35A return code is set by commands that may be used to determine control-flow,
36and the standard output from a command may be used
37as shell input.
38.LP
39The
40.ul
41shell
42can modify the environment
43in which commands run.
44Input and output can be redirected
45to files, and processes that communicate through `pipes'
46can be invoked.
47Commands are found by
48searching directories
49in the file system in a
50sequence that can be defined by the user.
51Commands can be read either from the terminal or from a file,
52which allows command procedures to be
53stored for later use.
54.AE
55.ds ST \v'.3m'\s+2*\s0\v'-.3m'
56.SH
571.0\ Introduction
58.LP
59The shell is both a command language
60and a programming language
61that provides an interface to the UNIX
62operating system.
63This memorandum describes, with
64examples, the UNIX shell.
65The first section covers most of the
66everyday requirements
67of terminal users.
68Some familiarity with UNIX
69is an advantage when reading this section;
70see, for example,
71"UNIX for beginners".
72.[
73unix beginn kernigh 1978
74.]
75Section 2 describes those features
76of the shell primarily intended
77for use within shell procedures.
78These include the control-flow
79primitives and string-valued variables
80provided by the shell.
81A knowledge of a programming language
82would be a help when reading this section.
83The last section describes the more
84advanced features of the shell.
85References of the form "see \fIpipe\fP (2)"
86are to a section of the UNIX manual.
87.[
88seventh 1978 ritchie thompson
89.]
90.SH
911.1\ Simple\ commands
92.LP
93Simple commands consist of one or more words
94separated by blanks.
95The first word is the name of the command
96to be executed; any remaining words
97are passed as arguments to the command.
98For example,
99.DS
100 who
101.DE
102is a command that prints the names
103of users logged in.
104The command
105.DS
106 ls \(mil
107.DE
108prints a list of files in the current
109directory.
110The argument \fI\(mil\fP tells \fIls\fP
111to print status information, size and
112the creation date for each file.
113.SH
1141.2\ Background\ commands
115.LP
116To execute a command the shell normally
117creates a new \fIprocess\fP
118and waits for it to finish.
119A command may be run without waiting
120for it to finish.
121For example,
122.DS
123 cc pgm.c &
124.DE
125calls the C compiler to compile
126the file \fIpgm.c\|.\fP
127The trailing \fB&\fP is an operator that instructs the shell
128not to wait for the command to finish.
129To help keep track of such a process
130the shell reports its process
131number following its creation.
132A list of currently active processes may be obtained
133using the \fIps\fP command.
134.SH
1351.3\ Input\ output\ redirection
136.LP
137Most commands produce output on the standard output
138that is initially connected to the terminal.
139This output may be sent to a file
140by writing, for example,
141.DS
142 ls \(mil >file
143.DE
144The notation \fI>file\fP
145is interpreted by the shell and is not passed
146as an argument to \fIls.\fP
147If \fIfile\fP does not exist then the
148shell creates it;
149otherwise the original contents of
150\fIfile\fP are replaced with the output
151from \fIls.\fP
152Output may be appended to a file
153using the notation
154.DS
155 ls \(mil \*(APfile
156.DE
157In this case \fIfile\fP is also created if it does not already
158exist.
159.LP
160The standard input of a command may be taken
161from a file instead of the terminal by
162writing, for example,
163.DS
164 wc <file
165.DE
166The command \fIwc\fP reads its standard input
167(in this case redirected from \fIfile\fP)
168and prints the number of characters, words and
169lines found.
170If only the number of lines is required
171then
172.DS
173 wc \(mil <file
174.DE
175could be used.
176.SH
1771.4\ Pipelines\ and\ filters
178.LP
179The standard output of one command may be
180connected to the standard input of another
181by writing
182the `pipe' operator,
183indicated by \*(VT,
184as in,
185.DS
186 ls \(mil \*(VT wc
187.DE
188Two commands connected in this way constitute
189a \fIpipeline\fP and
190the overall effect is the same as
191.DS
192 ls \(mil >file; wc <file
193.DE
194except that no \fIfile\fP is used.
195Instead the two processes are connected
196by a pipe (see \fIpipe\fP (2)) and are
197run in parallel.
198Pipes are unidirectional and
199synchronization is achieved by
200halting \fIwc\fP when there is
201nothing to read and halting \fIls\fP
202when the pipe is full.
203.LP
204A \fIfilter\fP is a command
205that reads its standard input,
206transforms it in some way,
207and prints the result as output.
208One such filter, \fIgrep,\fP
209selects from its input those lines
210that contain some specified string.
211For example,
212.DS
213 ls \*(VT grep old
214.DE
215prints those lines, if any, of the output
216from \fIls\fP that contain
217the string \fIold.\fP
218Another useful filter is \fIsort\fP.
219For example,
220.DS
221 who \*(VT sort
222.DE
223will print an alphabetically sorted list
224of logged in users.
225.LP
226A pipeline may consist of more than two commands,
227for example,
228.DS
229 ls \*(VT grep old \*(VT wc \(mil
230.DE
231prints the number of file names
232in the current directory containing
233the string \fIold.\fP
234.SH
2351.5\ File\ name\ generation
236.LP
237Many commands accept arguments
238which are file names.
239For example,
240.DS
241 ls \(mil main.c
242.DE
243prints information relating to the file \fImain.c\fP\|.
244.LP
245The shell provides a mechanism
246for generating a list of file names
247that match a pattern.
248For example,
249.DS
250 ls \(mil \*(ST.c
251.DE
252generates, as arguments to \fIls,\fP
253all file names in the current directory that end in \fI.c\|.\fP
254The character \*(ST is a pattern that will match any string
255including the null string.
256In general \fIpatterns\fP are specified
257as follows.
258.RS
259.IP \fB\*(ST\fR 8
260Matches any string of characters
261including the null string.
262.IP \fB?\fR 8
263Matches any single character.
264.IP \fB[\*(ZZ]\fR 8
265Matches any one of the characters
266enclosed.
267A pair of characters separated by a minus will
268match any character lexically between
269the pair.
270.RE
271.LP
272For example,
273.DS
274 [a\(miz]\*(ST
275.DE
276matches all names in the current directory
277beginning with
278one of the letters \fIa\fP through \fIz.\fP
279.DS
280 /usr/fred/test/?
281.DE
282matches all names in the directory
283\fB/usr/fred/test\fP that consist of a single character.
284If no file name is found that matches
285the pattern then the pattern is passed,
286unchanged, as an argument.
287.LP
288This mechanism is useful both to save typing
289and to select names according to some pattern.
290It may also be used to find files.
291For example,
292.DS
293 echo /usr/fred/\*(ST/core
294.DE
295finds and prints the names of all \fIcore\fP files in sub-directories
296of \fB/usr/fred\|.\fP
297(\fIecho\fP is a standard UNIX command that prints
298its arguments, separated by blanks.)
299This last feature can be expensive,
300requiring a scan of all
301sub-directories of \fB/usr/fred\|.\fP
302.LP
303There is one exception to the general
304rules given for patterns.
305The character `\fB.\fP'
306at the start of a file name must be explicitly
307matched.
308.DS
309 echo \*(ST
310.DE
311will therefore echo all file names in the current
312directory not beginning
313with `\fB.\fP'\|.
314.DS
315 echo \fB.\fP\*(ST
316.DE
317will echo all those file names that begin with `\fB.\fP'\|.
318This avoids inadvertent matching
319of the names `\fB.\fP' and `\fB..\fP'
320which mean `the current directory'
321and `the parent directory'
322respectively.
323(Notice that \fIls\fP suppresses
324information for the files `\fB.\fP' and `\fB..\fP'\|.)
325.SH
3261.6\ Quoting
327.LP
328Characters that have a special meaning
329to the shell, such as \fB< > \*(ST ? \*(VT &\|,\fR
330are called metacharacters.
331A complete list of metacharacters is given
332in appendix B.
333Any character preceded by a \fB\\\fR is \fIquoted\fP
334and loses its special meaning, if any.
335The \fB\\\fP is elided so that
336.DS
337 echo \\\\?
338.DE
339will echo a single \fB?\|,\fP
340and
341.DS
342 echo \\\\\\\\
343.DE
344will echo a single \fB\\\|.\fR
345To allow long strings to be continued over
346more than one line
347the sequence \fB\\newline\fP
348is ignored.
349.LP
350\fB\\\fP is convenient for quoting
351single characters.
352When more than one character needs
353quoting the above mechanism is clumsy and
354error prone.
355A string of characters may be quoted
356by enclosing the string between single quotes.
357For example,
358.DS
359 echo xx\'\*(ST\*(ST\*(ST\*(ST\'xx
360.DE
361will echo
362.DS
363 xx\*(ST\*(ST\*(ST\*(STxx
364.DE
365The quoted string may not contain
366a single quote
367but may contain newlines, which are preserved.
368This quoting mechanism is the most
369simple and is recommended
370for casual use.
371.LP
372A third quoting mechanism using double quotes
373is also available
374that prevents interpretation of some but not all
375metacharacters.
376Discussion of the
377details is deferred
378to section 3.4\|.
379.SH
3801.7\ Prompting
381.LP
382When the shell is used from a terminal it will
383issue a prompt before reading a command.
384By default this prompt is `\fB$\ \fR'\|.
385It may be changed by saying,
386for example,
387.DS
388 \s-1PS1\s0=yesdear
389.DE
390that sets the prompt to be the string \fIyesdear\|.\fP
391If a newline is typed and further input is needed
392then the shell will issue the prompt `\fB>\ \fR'\|.
393Sometimes this can be caused by mistyping
394a quote mark.
395If it is unexpected then an interrupt (\s-1DEL\s0)
396will return the shell to read another command.
397This prompt may be changed by saying, for example,
398.DS
399 \s-1PS2\s0=more
400.DE
401.SH
4021.8\ The\ shell\ and\ login
403.LP
404Following \fIlogin\fP (1)
405the shell is called to read and execute
406commands typed at the terminal.
407If the user's login directory
408contains the file \fB.profile\fP
409then it is assumed to contain commands
410and is read by the shell before reading
411any commands from the terminal.
412.SH
4131.9\ Summary
414.sp
415.RS
416.IP \(bu
417\fBls\fP
418.br
419Print the names of files in the current directory.
420.IP \(bu
421\fBls >file\fP
422.br
423Put the output from \fIls\fP into \fIfile.\fP
424.IP \(bu
425\fBls \*(VT wc \(mil\fR
426.br
427Print the number of files in the current directory.
428.IP \(bu
429\fBls \*(VT grep old\fR
430.br
431Print those file names containing the string \fIold.\fP
432.IP \(bu
433\fBls \*(VT grep old \*(VT wc \(mil\fR
434.br
435Print the number of files whose name contains the string \fIold.\fP
436.IP \(bu
437\fBcc pgm.c &\fR
438.br
439Run \fIcc\fP in the background.
440.RE