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