Commit | Line | Data |
---|---|---|
c9528a00 C |
1 | .bp |
2 | .SH | |
3 | 3.0\ Keyword\ parameters | |
4 | .LP | |
5 | Shell variables may be given values | |
6 | by assignment | |
7 | or when a shell procedure is invoked. | |
8 | An argument to a shell procedure of the form | |
9 | \fIname=value\fP | |
10 | that precedes the command name | |
11 | causes \fIvalue\fP | |
12 | to be assigned to \fIname\fP | |
13 | before execution of the procedure begins. | |
14 | The value of \fIname\fP in the invoking | |
15 | shell is not affected. | |
16 | For example, | |
17 | .DS | |
18 | user=fred\ command | |
19 | .DE | |
20 | will execute \fIcommand\fP with | |
21 | \fBuser\fP set to \fIfred\fP. | |
22 | The \fB\(mik\fR flag causes arguments of the form | |
23 | \fIname=value\fP to be interpreted in this way | |
24 | anywhere in the argument list. | |
25 | Such \fInames\fP are sometimes | |
26 | called keyword parameters. | |
27 | If any arguments remain they | |
28 | are available as positional | |
29 | parameters \fB$1, $2, \*(ZZ\|.\fP | |
30 | .LP | |
31 | The \fIset\fP command | |
32 | may also be used to set positional parameters | |
33 | from within a procedure. | |
34 | For example, | |
35 | .DS | |
36 | set\ \(mi\ \*(ST | |
37 | .DE | |
38 | will set \fB$1\fP to the first file name | |
39 | in the current directory, \fB$2\fP to the next, | |
40 | and so on. | |
41 | Note that the first argument, \(mi, ensures correct treatment | |
42 | when the first file name begins with a \(mi\|. | |
43 | .LP | |
44 | .SH | |
45 | 3.1\ Parameter\ transmission | |
46 | .LP | |
47 | When a shell procedure is invoked both positional | |
48 | and keyword parameters may be supplied with the call. | |
49 | Keyword parameters are also made available implicitly | |
50 | to a shell procedure | |
51 | by specifying in advance that such parameters | |
52 | are to be exported. | |
53 | For example, | |
54 | .DS | |
55 | export\ user\ box | |
56 | .DE | |
57 | marks the variables \fBuser\fP and \fBbox\fP | |
58 | for export. | |
59 | When a shell procedure is invoked | |
60 | copies are made of all exportable variables | |
61 | for use within the invoked procedure. | |
62 | Modification of such variables | |
63 | within the procedure does not | |
64 | affect the values in the invoking shell. | |
65 | It is generally true of | |
66 | a shell procedure | |
67 | that it | |
68 | may not modify the state | |
69 | of its caller without explicit | |
70 | request on the part of the caller. | |
71 | (Shared file descriptors are an | |
72 | exception to this rule.) | |
73 | .LP | |
74 | Names whose value is intended to remain | |
75 | constant may be declared \fIreadonly\|.\fP | |
76 | The form of this command is the same as that of the \fIexport\fP | |
77 | command, | |
78 | .DS | |
79 | readonly name \*(ZZ | |
80 | .DE | |
81 | Subsequent attempts to set readonly variables | |
82 | are illegal. | |
83 | .SH | |
84 | 3.2\ Parameter\ substitution | |
85 | .LP | |
86 | If a shell parameter is not set | |
87 | then the null string is substituted for it. | |
88 | For example, if the variable \fBd\fP | |
89 | is not set | |
90 | .DS | |
91 | echo $d | |
92 | .DE | |
93 | or | |
94 | .DS | |
95 | echo ${d} | |
96 | .DE | |
97 | will echo nothing. | |
98 | A default string may be given | |
99 | as in | |
100 | .DS | |
101 | echo ${d\(mi\fB.\fR} | |
102 | .DE | |
103 | which will echo | |
104 | the value of the variable \fBd\fP | |
105 | if it is set and `\fB.\fP' otherwise. | |
106 | The default string is evaluated using the usual | |
107 | quoting conventions so that | |
108 | .DS | |
109 | echo ${d\(mi\'\*(ST\'} | |
110 | .DE | |
111 | will echo \fB\*(ST\fP if the variable \fBd\fP | |
112 | is not set. | |
113 | Similarly | |
114 | .DS | |
115 | echo ${d\(mi$1} | |
116 | .DE | |
117 | will echo the value of \fBd\fP if it is set | |
118 | and the value (if any) of \fB$1\fP otherwise. | |
119 | A variable may be assigned a default value | |
120 | using | |
121 | the notation | |
122 | .DS | |
123 | echo ${d=\fB.\fR} | |
124 | .DE | |
125 | which substitutes the same string as | |
126 | .DS | |
127 | echo ${d\(mi\fB.\fR} | |
128 | .DE | |
129 | and if \fBd\fP were not previously set | |
130 | then it will be set to the string `\fB.\fP'\|. | |
131 | (The notation ${\*(ZZ=\*(ZZ} | |
132 | is not available for positional parameters.) | |
133 | .LP | |
134 | If there is no sensible default then | |
135 | the notation | |
136 | .DS | |
137 | echo ${d?message} | |
138 | .DE | |
139 | will echo the value of the variable \fBd\fP if it has | |
140 | one, otherwise \fImessage\fP is printed by the shell and | |
141 | execution of the shell procedure is abandoned. | |
142 | If \fImessage\fP is absent then a standard message | |
143 | is printed. | |
144 | A shell procedure that requires some parameters | |
145 | to be set might start as follows. | |
146 | .DS | |
147 | :\ ${user?}\ ${acct?}\ ${bin?} | |
148 | \*(ZZ | |
149 | .DE | |
150 | Colon (\fB:\fP) is a command | |
151 | that is | |
152 | built in to the shell and does nothing | |
153 | once its arguments have been evaluated. | |
154 | If any of the variables \fBuser, acct\fP | |
155 | or \fBbin\fP are not set then the shell | |
156 | will abandon execution of the procedure. | |
157 | .SH | |
158 | 3.3\ Command\ substitution | |
159 | .LP | |
160 | The standard output from a command can be | |
161 | substituted in a similar way to parameters. | |
162 | The command \fIpwd\fP prints on its standard | |
163 | output the name of the current directory. | |
164 | For example, if the current directory is | |
165 | \fB/usr/fred/bin\fR | |
166 | then the command | |
167 | .DS | |
168 | d=\`pwd\` | |
169 | .DE | |
170 | is equivalent to | |
171 | .DS | |
172 | d=/usr/fred/bin | |
173 | .DE | |
174 | .LP | |
175 | The entire string between grave accents (\`\*(ZZ\`) | |
176 | is taken as the command | |
177 | to be executed | |
178 | and is replaced with the output from | |
179 | the command. | |
180 | The command is written using the usual | |
181 | quoting conventions | |
182 | except that a \fB\`\fR must be escaped using | |
183 | a \fB\\\|.\fR | |
184 | For example, | |
185 | .DS | |
186 | ls \`echo "$1"\` | |
187 | .DE | |
188 | is equivalent to | |
189 | .DS | |
190 | ls $1 | |
191 | .DE | |
192 | Command substitution occurs in all contexts | |
193 | where parameter substitution occurs (including \fIhere\fP documents) and the | |
194 | treatment of the resulting text is the same | |
195 | in both cases. | |
196 | This mechanism allows string | |
197 | processing commands to be used within | |
198 | shell procedures. | |
199 | An example of such a command is \fIbasename\fP | |
200 | which removes a specified suffix from a string. | |
201 | For example, | |
202 | .DS | |
203 | basename main\fB.\fPc \fB.\fPc | |
204 | .DE | |
205 | will print the string \fImain\|.\fP | |
206 | Its use is illustrated by the following | |
207 | fragment from a \fIcc\fP command. | |
208 | .DS | |
209 | case $A in | |
210 | \*(Ca\*(ZZ | |
211 | \*(Ca\*(ST\fB.\fPc) B=\`basename $A \fB.\fPc\` | |
212 | \*(Ca\*(ZZ | |
213 | esac | |
214 | .DE | |
215 | that sets \fBB\fP to the part of \fB$A\fP | |
216 | with the suffix \fB.c\fP stripped. | |
217 | .LP | |
218 | Here are some composite examples. | |
219 | .RS | |
220 | .IP \(bu | |
221 | .ft B | |
222 | for i in \`ls \(mit\`; do \*(ZZ | |
223 | .ft R | |
224 | .br | |
225 | The variable \fBi\fP is set | |
226 | to the names of files in time order, | |
227 | most recent first. | |
228 | .IP \(bu | |
229 | .ft B | |
230 | set \`date\`; echo $6 $2 $3, $4 | |
231 | .ft R | |
232 | .br | |
233 | will print, e.g., | |
234 | .ft I | |
235 | 1977 Nov 1, 23:59:59 | |
236 | .ft R | |
237 | .RE | |
238 | .SH | |
239 | 3.4\ Evaluation\ and\ quoting | |
240 | .LP | |
241 | The shell is a macro processor that | |
242 | provides parameter substitution, command substitution and file | |
243 | name generation for the arguments to commands. | |
244 | This section discusses the order in which | |
245 | these evaluations occur and the | |
246 | effects of the various quoting mechanisms. | |
247 | .LP | |
248 | Commands are parsed initially according to the grammar | |
249 | given in appendix A. | |
250 | Before a command is executed | |
251 | the following | |
252 | substitutions occur. | |
253 | .RS | |
254 | .IP \(bu | |
255 | parameter substitution, e.g. \fB$user\fP | |
256 | .IP \(bu | |
257 | command substitution, e.g. \fB\`pwd\`\fP | |
258 | .RS | |
259 | .LP | |
260 | Only one evaluation occurs so that if, for example, the value of the variable | |
261 | \fBX\fP | |
262 | is the string \fI$y\fP | |
263 | then | |
264 | .DS | |
265 | echo $X | |
266 | .DE | |
267 | will echo \fI$y\|.\fP | |
268 | .RE | |
269 | .IP \(bu | |
270 | blank interpretation | |
271 | .RS | |
272 | .LP | |
273 | Following the above substitutions | |
274 | the resulting characters | |
275 | are broken into non-blank words (\fIblank interpretation\fP). | |
276 | For this purpose `blanks' are the characters of the string | |
277 | \fB$\s-1IFS\s0\fP. | |
278 | By default, this string consists of blank, tab and newline. | |
279 | The null string | |
280 | is not regarded as a word unless it is quoted. | |
281 | For example, | |
282 | .DS | |
283 | echo \'\' | |
284 | .DE | |
285 | will pass on the null string as the first argument to \fIecho\fP, | |
286 | whereas | |
287 | .DS | |
288 | echo $null | |
289 | .DE | |
290 | will call \fIecho\fR with no arguments | |
291 | if the variable \fBnull\fP is not set | |
292 | or set to the null string. | |
293 | .RE | |
294 | .IP \(bu | |
295 | file name generation | |
296 | .RS | |
297 | .LP | |
298 | Each word | |
299 | is then scanned for the file pattern characters | |
300 | \fB\*(ST, ?\fR and \fB[\*(ZZ]\fR | |
301 | and an alphabetical list of file names | |
302 | is generated to replace the word. | |
303 | Each such file name is a separate argument. | |
304 | .RE | |
305 | .RE | |
306 | .LP | |
307 | The evaluations just described also occur | |
308 | in the list of words associated with a \fBfor\fP | |
309 | loop. | |
310 | Only substitution occurs | |
311 | in the \fIword\fP used | |
312 | for a \fBcase\fP branch. | |
313 | .LP | |
314 | As well as the quoting mechanisms described | |
315 | earlier using \fB\\\fR and \fB\'\*(ZZ\'\fR | |
316 | a third quoting mechanism is provided using double quotes. | |
317 | Within double quotes parameter and command substitution | |
318 | occurs but file name generation and the interpretation | |
319 | of blanks does not. | |
320 | The following characters | |
321 | have a special meaning within double quotes | |
322 | and may be quoted using \fB\\\|.\fP | |
323 | .DS | |
324 | \fB$ \fPparameter substitution | |
325 | \fB\`\fP command substitution | |
326 | \fB"\fP ends the quoted string | |
327 | \fB\e\fP quotes the special characters \fB$ \` " \e\fP | |
328 | .DE | |
329 | For example, | |
330 | .DS | |
331 | echo "$x" | |
332 | .DE | |
333 | will pass the value of the variable \fBx\fP as a | |
334 | single argument to \fIecho.\fP | |
335 | Similarly, | |
336 | .DS | |
337 | echo "$\*(ST" | |
338 | .DE | |
339 | will pass the positional parameters as a single | |
340 | argument and is equivalent to | |
341 | .DS | |
342 | echo "$1 $2 \*(ZZ" | |
343 | .DE | |
344 | The notation \fB$@\fP | |
345 | is the same as \fB$\*(ST\fR | |
346 | except when it is quoted. | |
347 | .DS | |
348 | echo "$@" | |
349 | .DE | |
350 | will pass the positional parameters, unevaluated, to \fIecho\fR | |
351 | and is equivalent to | |
352 | .DS | |
353 | echo "$1" "$2" \*(ZZ | |
354 | .DE | |
355 | .LP | |
356 | The following table gives, for each quoting mechanism, | |
357 | the shell metacharacters that are evaluated. | |
358 | .DS | |
359 | .ce | |
360 | .ft I | |
361 | metacharacter | |
362 | .ft | |
363 | .in 1.5i | |
364 | \e $ * \` " \' | |
365 | \' n n n n n t | |
366 | \` y n n t n n | |
367 | " y y n y t n | |
368 | ||
369 | t terminator | |
370 | y interpreted | |
371 | n not interpreted | |
372 | ||
373 | .in | |
374 | .ft B | |
375 | .ce | |
376 | Figure 2. Quoting mechanisms | |
377 | .ft | |
378 | .DE | |
379 | .LP | |
380 | In cases where more than one evaluation of a string | |
381 | is required the built-in command \fIeval\fP | |
382 | may be used. | |
383 | For example, | |
384 | if the variable \fBX\fP has the value | |
385 | \fI$y\fP, and if \fBy\fP has the value \fIpqr\fP | |
386 | then | |
387 | .DS | |
388 | eval echo $X | |
389 | .DE | |
390 | will echo the string \fIpqr\|.\fP | |
391 | .LP | |
392 | In general the \fIeval\fP command | |
393 | evaluates its arguments (as do all commands) | |
394 | and treats the result as input to the shell. | |
395 | The input is read and the resulting command(s) | |
396 | executed. | |
397 | For example, | |
398 | .DS | |
399 | wg=\\'eval who\*(VTgrep\\' | |
400 | $wg fred | |
401 | .DE | |
402 | is equivalent to | |
403 | .DS | |
404 | who\*(VTgrep fred | |
405 | .DE | |
406 | In this example, | |
407 | \fIeval\fP is required | |
408 | since there is no interpretation | |
409 | of metacharacters, such as \fB\*(VT\|,\fP following | |
410 | substitution. | |
411 | .SH | |
412 | 3.5\ Error\ handling | |
413 | .LP | |
414 | The treatment of errors detected by | |
415 | the shell depends on the type of error | |
416 | and on whether the shell is being | |
417 | used interactively. | |
418 | An interactive shell is one whose | |
419 | input and output are connected | |
420 | to a terminal (as determined by | |
421 | \fIgtty\fP (2)). | |
422 | A shell invoked with the \fB\(mii\fP | |
423 | flag is also interactive. | |
424 | .LP | |
425 | Execution of a command (see also 3.7) may fail | |
426 | for any of the following reasons. | |
427 | .IP \(bu | |
428 | Input output redirection may fail. | |
429 | For example, if a file does not exist | |
430 | or cannot be created. | |
431 | .IP \(bu | |
432 | The command itself does not exist | |
433 | or cannot be executed. | |
434 | .IP \(bu | |
435 | The command terminates abnormally, | |
436 | for example, with a "bus error" | |
437 | or "memory fault". | |
438 | See Figure 2 below for a complete list | |
439 | of UNIX signals. | |
440 | .IP \(bu | |
441 | The command terminates normally | |
442 | but returns a non-zero exit status. | |
443 | .LP | |
444 | In all of these cases the shell | |
445 | will go on to execute the next command. | |
446 | Except for the last case an error | |
447 | message will be printed by the shell. | |
448 | All remaining errors cause the shell | |
449 | to exit from a command procedure. | |
450 | An interactive shell will return | |
451 | to read another command from the terminal. | |
452 | Such errors include the following. | |
453 | .IP \(bu | |
454 | Syntax errors. | |
455 | e.g., if \*(ZZ then \*(ZZ done | |
456 | .IP \(bu | |
457 | A signal such as interrupt. | |
458 | The shell waits for the current | |
459 | command, if any, to finish execution and | |
460 | then either exits or returns to the terminal. | |
461 | .IP \(bu | |
462 | Failure of any of the built-in commands | |
463 | such as \fIcd.\fP | |
464 | .LP | |
465 | The shell flag \fB\(mie\fP | |
466 | causes the shell to terminate | |
467 | if any error is detected. | |
468 | .DS | |
469 | 1 hangup | |
470 | 2 interrupt | |
471 | 3* quit | |
472 | 4* illegal instruction | |
473 | 5* trace trap | |
474 | 6* IOT instruction | |
475 | 7* EMT instruction | |
476 | 8* floating point exception | |
477 | 9 kill (cannot be caught or ignored) | |
478 | 10* bus error | |
479 | 11* segmentation violation | |
480 | 12* bad argument to system call | |
481 | 13 write on a pipe with no one to read it | |
482 | 14 alarm clock | |
483 | 15 software termination (from \fIkill\fP (1)) | |
484 | ||
485 | .DE | |
486 | .ft B | |
487 | .ce | |
488 | Figure 3. UNIX signals | |
489 | .ft | |
490 | ||
491 | Those signals marked with an asterisk | |
492 | produce a core dump | |
493 | if not caught. | |
494 | However, | |
495 | the shell itself ignores quit which is the only | |
496 | external signal that can cause a dump. | |
497 | The signals in this list of potential interest | |
498 | to shell programs are 1, 2, 3, 14 and 15. | |
499 | .SH | |
500 | 3.6\ Fault\ handling | |
501 | .LP | |
502 | Shell procedures normally terminate | |
503 | when an interrupt is received from the | |
504 | terminal. | |
505 | The \fItrap\fP command is used | |
506 | if some cleaning up is required, such | |
507 | as removing temporary files. | |
508 | For example, | |
509 | .DS | |
510 | trap\ \'rm\ /tmp/ps$$; exit\'\ 2 | |
511 | .DE | |
512 | sets a trap for signal 2 (terminal | |
513 | interrupt), and if this signal is received | |
514 | will execute the commands | |
515 | .DS | |
516 | rm /tmp/ps$$; exit | |
517 | .DE | |
518 | \fIexit\fP is | |
519 | another built-in command | |
520 | that terminates execution of a shell procedure. | |
521 | The \fIexit\fP is required; otherwise, | |
522 | after the trap has been taken, | |
523 | the shell will resume executing | |
524 | the procedure | |
525 | at the place where it was interrupted. | |
526 | .LP | |
527 | UNIX signals can be handled in one of three ways. | |
528 | They can be ignored, in which case | |
529 | the signal is never sent to the process. | |
530 | They can be caught, in which case the process | |
531 | must decide what action to take when the | |
532 | signal is received. | |
533 | Lastly, they can be left to cause | |
534 | termination of the process without | |
535 | it having to take any further action. | |
536 | If a signal is being ignored | |
537 | on entry to the shell procedure, for example, | |
538 | by invoking it in the background (see 3.7) then \fItrap\fP | |
539 | commands (and the signal) are ignored. | |
540 | .LP | |
541 | The use of \fItrap\fP is illustrated | |
542 | by this modified version of the \fItouch\fP | |
543 | command (Figure 4). | |
544 | The cleanup action is to remove the file \fBjunk$$\fR\|. | |
545 | .DS | |
546 | flag= | |
547 | trap\ \'rm\ \(mif\ junk$$;\ exit\'\ 1 2 3 15 | |
548 | for i | |
549 | do\ case\ $i\ in | |
550 | \*(DC\(mic) flag=N ;; | |
551 | \*(DC\*(ST) if\ test\ \(mif\ $i | |
552 | \*(DC then ln\ $i\ junk$$;\ rm\ junk$$ | |
553 | \*(DC elif\ test\ $flag | |
554 | \*(DC then echo\ file\ \\\\\'$i\\\\\'\ does\ not\ exist | |
555 | \*(DC else >$i | |
556 | \*(DC fi | |
557 | \*(DOesac | |
558 | done | |
559 | .DE | |
560 | .sp | |
561 | .ft B | |
562 | .ce | |
563 | Figure 4. The touch command | |
564 | .ft | |
565 | .sp | |
566 | The \fItrap\fP command | |
567 | appears before the creation | |
568 | of the temporary file; | |
569 | otherwise it would be | |
570 | possible for the process | |
571 | to die without removing | |
572 | the file. | |
573 | .LP | |
574 | Since there is no signal 0 in UNIX | |
575 | it is used by the shell to indicate the | |
576 | commands to be executed on exit from the | |
577 | shell procedure. | |
578 | .LP | |
579 | A procedure may, itself, elect to | |
580 | ignore signals by specifying the null | |
581 | string as the argument to trap. | |
582 | The following fragment is taken from the | |
583 | \fInohup\fP command. | |
584 | .DS | |
585 | trap \'\' 1 2 3 15 | |
586 | .DE | |
587 | which causes \fIhangup, interrupt, quit \fRand\fI kill\fR | |
588 | to be ignored both by the | |
589 | procedure and by invoked commands. | |
590 | .LP | |
591 | Traps may be reset by saying | |
592 | .DS | |
593 | trap 2 3 | |
594 | .DE | |
595 | which resets the traps for signals 2 and 3 to their default values. | |
596 | A list of the current values of traps may be obtained | |
597 | by writing | |
598 | .DS | |
599 | trap | |
600 | .DE | |
601 | .LP | |
602 | The procedure \fIscan\fP (Figure 5) is an example | |
603 | of the use of \fItrap\fP where there is no exit | |
604 | in the trap command. | |
605 | \fIscan\fP takes each directory | |
606 | in the current directory, prompts | |
607 | with its name, and then executes | |
608 | commands typed at the terminal | |
609 | until an end of file or an interrupt is received. | |
610 | Interrupts are ignored while executing | |
611 | the requested commands but cause | |
612 | termination when \fIscan\fP is | |
613 | waiting for input. | |
614 | .DS | |
615 | d=\`pwd\` | |
616 | for\ i\ in\ \*(ST | |
617 | do\ if\ test\ \(mid\ $d/$i | |
618 | \*(DOthen\ cd\ $d/$i | |
619 | \*(DO\*(THwhile\ echo\ "$i:" | |
620 | \*(DO\*(TH\*(WHtrap\ exit\ 2 | |
621 | \*(DO\*(TH\*(WHread\ x | |
622 | \*(DO\*(THdo\ trap\ :\ 2;\ eval\ $x;\ done | |
623 | \*(DOfi | |
624 | done | |
625 | .DE | |
626 | .sp | |
627 | .ft B | |
628 | .ce | |
629 | Figure 5. The scan command | |
630 | .ft | |
631 | .sp | |
632 | \fIread x\fR is a built-in command that reads one line from the | |
633 | standard input | |
634 | and places the result in the variable \fBx\|.\fP | |
635 | It returns a non-zero exit status if either | |
636 | an end-of-file is read or an interrupt | |
637 | is received. | |
638 | .SH | |
639 | 3.7\ Command\ execution | |
640 | .LP | |
641 | To run a command (other than a built-in) the shell first creates | |
642 | a new process using the system call \fIfork.\fP | |
643 | The execution environment for the command | |
644 | includes input, output and the states of signals, and | |
645 | is established in the child process | |
646 | before the command is executed. | |
647 | The built-in command \fIexec\fP | |
648 | is used in the rare cases when no fork | |
649 | is required | |
650 | and simply replaces the shell with a new command. | |
651 | For example, a simple version of the \fInohup\fP | |
652 | command looks like | |
653 | .DS | |
654 | trap \\'\\' 1 2 3 15 | |
655 | exec $\*(ST | |
656 | .DE | |
657 | The \fItrap\fP turns off the signals specified | |
658 | so that they are ignored by subsequently created commands | |
659 | and \fIexec\fP replaces the shell by the command | |
660 | specified. | |
661 | .LP | |
662 | Most forms of input output redirection have already been | |
663 | described. | |
664 | In the following \fIword\fP is only subject | |
665 | to parameter and command substitution. | |
666 | No file name generation or blank interpretation | |
667 | takes place so that, for example, | |
668 | .DS | |
669 | echo \*(ZZ >\*(ST.c | |
670 | .DE | |
671 | will write its output into a file whose name is \fB\*(ST.c\|.\fP | |
672 | Input output specifications are evaluated left to right | |
673 | as they appear in the command. | |
674 | .IP >\ \fIword\fP 12 | |
675 | The standard output (file descriptor 1) | |
676 | is sent to the file \fIword\fP which is | |
677 | created if it does not already exist. | |
678 | .IP \*(AP\ \fIword\fP 12 | |
679 | The standard output is sent to file \fIword.\fP | |
680 | If the file exists then output is appended | |
681 | (by seeking to the end); | |
682 | otherwise the file is created. | |
683 | .IP <\ \fIword\fP 12 | |
684 | The standard input (file descriptor 0) | |
685 | is taken from the file \fIword.\fP | |
686 | .IP \*(HE\ \fIword\fP 12 | |
687 | The standard input is taken from the lines | |
688 | of shell input that follow up to but not | |
689 | including a line consisting only of \fIword.\fP | |
690 | If \fIword\fP is quoted then no interpretation | |
691 | of the document occurs. | |
692 | If \fIword\fP is not quoted | |
693 | then parameter and command substitution | |
694 | occur and \fB\\\fP is used to quote | |
695 | the characters \fB\\\fP \fB$\fP \fB\`\fP and the first character | |
696 | of \fIword.\fP | |
697 | In the latter case \fB\\newline\fP is ignored (c.f. quoted strings). | |
698 | .IP >&\ \fIdigit\fP 12 | |
699 | The file descriptor \fIdigit\fP is duplicated using the system | |
700 | call \fIdup\fP (2) | |
701 | and the result is used as the standard output. | |
702 | .IP <&\ \fIdigit\fP 12 | |
703 | The standard input is duplicated from file descriptor \fIdigit.\fP | |
704 | .IP <&\(mi 12 | |
705 | The standard input is closed. | |
706 | .IP >&\(mi 12 | |
707 | The standard output is closed. | |
708 | .LP | |
709 | Any of the above may be preceded by a digit in which | |
710 | case the file descriptor created is that specified by the digit | |
711 | instead of the default 0 or 1. | |
712 | For example, | |
713 | .DS | |
714 | \*(ZZ 2>file | |
715 | .DE | |
716 | runs a command with message output (file descriptor 2) | |
717 | directed to \fIfile.\fP | |
718 | .DS | |
719 | \*(ZZ 2>&1 | |
720 | .DE | |
721 | runs a command with its standard output and message output | |
722 | merged. | |
723 | (Strictly speaking file descriptor 2 is created | |
724 | by duplicating file descriptor 1 but the effect is usually to | |
725 | merge the two streams.) | |
726 | .LP | |
727 | The environment for a command run in the background such as | |
728 | .DS | |
729 | list \*(ST.c \*(VT lpr & | |
730 | .DE | |
731 | is modified in two ways. | |
732 | Firstly, the default standard input | |
733 | for such a command is the empty file \fB/dev/null\|.\fR | |
734 | This prevents two processes (the shell and the command), | |
735 | which are running in parallel, from trying to | |
736 | read the same input. | |
737 | Chaos would ensue | |
738 | if this were not the case. | |
739 | For example, | |
740 | .DS | |
741 | ed file & | |
742 | .DE | |
743 | would allow both the editor and the shell | |
744 | to read from the same input at the same time. | |
745 | .LP | |
746 | The other modification to the environment of a background | |
747 | command is to turn off the QUIT and INTERRUPT signals | |
748 | so that they are ignored by the command. | |
749 | This allows these signals to be used | |
750 | at the terminal without causing background | |
751 | commands to terminate. | |
752 | For this reason the UNIX convention | |
753 | for a signal is that if it is set to 1 | |
754 | (ignored) then it is never changed | |
755 | even for a short time. | |
756 | Note that the shell command \fItrap\fP | |
757 | has no effect for an ignored signal. | |
758 | .SH | |
759 | 3.8\ Invoking\ the\ shell | |
760 | .LP | |
761 | The following flags are interpreted by the shell | |
762 | when it is invoked. | |
763 | If the first character of argument zero is a minus, | |
764 | then commands are read from the file \fB.profile\|.\fP | |
765 | .IP \fB\(mic\fP\ \fIstring\fP | |
766 | .br | |
767 | If the \fB\(mic\fP flag is present then | |
768 | commands are read from \fIstring\|.\fP | |
769 | .IP \fB\(mis\fP | |
770 | If the \fB\(mis\fP flag is present or if no | |
771 | arguments remain | |
772 | then commands are read from the standard input. | |
773 | Shell output is written to | |
774 | file descriptor 2. | |
775 | .IP \fB\(mii\fP | |
776 | If the \fB\(mii\fP flag is present or | |
777 | if the shell input and output are attached to a terminal (as told by \fIgtty\fP) | |
778 | then this shell is \fIinteractive.\fP | |
779 | In this case TERMINATE is ignored (so that \fBkill 0\fP | |
780 | does not kill an interactive shell) and INTERRUPT is caught and ignored | |
781 | (so that \fBwait\fP is interruptable). | |
782 | In all cases QUIT is ignored by the shell. | |
783 | .SH | |
784 | Acknowledgements | |
785 | .LP | |
786 | The design of the shell is | |
787 | based in part on the original UNIX shell | |
788 | .[ | |
789 | unix command language thompson | |
790 | .] | |
791 | and the PWB/UNIX shell, | |
792 | .[ | |
793 | pwb shell mashey unix | |
794 | .] | |
795 | some | |
796 | features having been taken from both. | |
797 | Similarities also exist with the | |
798 | command interpreters | |
799 | of the Cambridge Multiple Access System | |
800 | .[ | |
801 | cambridge multiple access system hartley | |
802 | .] | |
803 | and of CTSS. | |
804 | .[ | |
805 | ctss | |
806 | .] | |
807 | .LP | |
808 | I would like to thank Dennis Ritchie | |
809 | and John Mashey for many | |
810 | discussions during the design of the shell. | |
811 | I am also grateful to the members of the Computing Science Research Center | |
812 | and to Joe Maranzano for their | |
813 | comments on drafts of this document. | |
814 | .SH | |
815 | .[ | |
816 | $LIST$ | |
817 | .] |