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