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