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