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