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