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