1 .\" $NetBSD: t3,v 1.3 2010/08/22 02:19:07 perry Exp $
3 .\" Copyright (C) Caldera International Inc. 2001-2002. All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions are
9 .\" Redistributions of source code and documentation must retain the above
10 .\" copyright notice, this list of conditions and the following
13 .\" Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
17 .\" All advertising materials mentioning features or use of this software
18 .\" must display the following acknowledgement:
20 .\" This product includes software developed or owned by Caldera
21 .\" International, Inc. Neither the name of Caldera International, Inc.
22 .\" nor the names of other contributors may be used to endorse or promote
23 .\" products derived from this software without specific prior written
26 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
27 .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
28 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 .\" DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
31 .\" FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34 .\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
36 .\" OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
37 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 .\" @(#)t3 8.1 (Berkeley) 6/8/93
42 3.0\ Keyword\ parameters
44 Shell variables may be given values
46 or when a shell script is invoked.
47 An argument to a command of the form
49 that precedes the command name
51 to be assigned to \fIname\fP
52 before execution of the command begins.
53 The value of \fIname\fP in the invoking
54 shell is not affected.
59 will execute \fIcommand\fP with
60 \fBuser\fP set to \fIfred\fP.
61 .\" Removed by Perry Metzger because -k is not in POSIX
63 .\" The \fB\(mik\fR flag causes arguments of the form
64 .\" \fIname=value\fP to be interpreted in this way
65 .\" anywhere in the argument list.
66 .\" Such \fInames\fP are sometimes
67 .\" called keyword parameters.
68 .\" If any arguments remain they
69 .\" are available as positional
70 .\" parameters \fB$1, $2, \*(ZZ\|.\fP
73 may also be used to set positional parameters
79 will set \fB$1\fP to the first file name
80 in the current directory, \fB$2\fP to the next,
82 Note that the first argument, \(mi\(mi, ensures correct treatment
83 when the first file name begins with a \(mi\|.
86 3.1\ Parameter\ transmission
88 When a command is executed both positional parameters
89 and shell variables may be set on invocation.
90 Variables are also made available implicitly
92 by specifying in advance that such parameters
93 are to be exported from the invoking shell.
98 marks the variables \fBuser\fP and \fBbox\fP
99 for export (setting \fBbox\fP to ``red'' in the process).
100 When a command is invoked
101 copies are made of all exportable variables
102 (also known as \fIenvironment variables\fP)
103 for use within the invoked program.
104 Modification of such variables
105 within an invoked command does not
106 affect the values in the invoking shell.
107 It is generally true of
108 a shell script or other program
110 cannot modify the state
111 of its caller without explicit
112 actions on the part of the caller.
113 .\" Removed by Perry Metzger because this is confusing to beginners.
115 .\" (Shared file descriptors are an
116 .\" exception to this rule.)
118 Names whose value is intended to remain
119 constant may be declared \fIreadonly\|.\fP
120 The form of this command is the same as that of the \fIexport\fP
123 readonly name[=value] \*(ZZ
125 Subsequent attempts to set readonly variables
128 3.2\ Parameter\ substitution
130 If a shell parameter is not set
131 then the null string is substituted for it.
132 For example, if the variable \fBd\fP
142 A default string may be given
145 echo ${d:\(mi\fB.\fR}
148 the value of the variable \fBd\fP
149 if it is set and not null and `\fB.\fP' otherwise.
150 The default string is evaluated using the usual
151 quoting conventions so that
153 echo ${d:\(mi\'\*(ST\'}
155 will echo \fB\*(ST\fP if the variable \fBd\fP
161 will echo the value of \fBd\fP if it is set and not null
162 and the value (if any) of \fB$1\fP otherwise.
164 The notation ${d:+\fB.\fR} performs the inverse operation. It
165 substitutes `\fB.\fP' if \fBd\fP is set or not null, and otherwise
168 A variable may be assigned a default value
174 which substitutes the same string as
176 echo ${d:\(mi\fB.\fR}
178 and if \fBd\fP were not previously set or null
179 then it will be set to the string `\fB.\fP'\|.
181 If there is no sensible default then
184 echo ${d:?\fImessage\fP}
186 will echo the value of the variable \fBd\fP if it is set and not null,
187 otherwise \fImessage\fP is printed by the shell and
188 execution of the shell script is abandoned.
189 If \fImessage\fP is absent then a standard message
191 A shell script that requires some variables
192 to be set might start as follows:
194 :\ ${user:?}\ ${acct:?}\ ${bin:?}
197 Colon (\fB:\fP) is a command
199 built in to the shell and does nothing
200 once its arguments have been evaluated.
201 If any of the variables \fBuser, acct\fP
202 or \fBbin\fP are not set then the shell
203 will abandon execution of the script.
205 3.3\ Command\ substitution
207 The standard output from a command can be
208 substituted in a similar way to parameters.
209 The command \fIpwd\fP prints on its standard
210 output the name of the current directory.
211 For example, if the current directory is
217 (or the older notation d=\`pwd\`)
223 The entire string inside $(\*(ZZ)\| (or between grave accents \`\*(ZZ\`)
224 is taken as the command
226 and is replaced with the output from
228 (The difference between the $(\*(ZZ) and \`\*(ZZ\` notations is that
229 the former may be nested, while the latter cannot be.)
231 The command is written using the usual quoting conventions,
232 except that inside \`\*(ZZ\`
233 a \fB\`\fR must be escaped using
243 Command substitution occurs in all contexts
244 where parameter substitution occurs (including \fIhere\fP documents) and the
245 treatment of the resulting text is the same
247 This mechanism allows string
248 processing commands to be used within
250 An example of such a command is \fIbasename\fP
251 which removes a specified suffix from a string.
254 basename main\fB.\fPc \fB.\fPc
256 will print the string \fImain\|.\fP
257 Its use is illustrated by the following
258 fragment from a \fIcc\fP command.
262 \*(Ca\*(ST\fB.\fPc) B=$(basename $A \fB.\fPc)
266 that sets \fBB\fP to the part of \fB$A\fP
267 with the suffix \fB.c\fP stripped.
269 Here are some composite examples.
273 for i in \`ls \(mit\`; do \*(ZZ
276 The variable \fBi\fP is set
277 to the names of files in time order,
281 set \(mi\(mi\| \`date\`; echo $6 $2 $3, $4
290 3.4\ Arithmetic\ Expansion
292 Within a $((\*(ZZ)) construct, integer arithmetic operations are
294 (The $ in front of variable names is optional within $((\*(ZZ)).
302 will print `11', then `6', then `6' again.
303 Most of the constructs permitted in C arithmetic operations are
304 permitted though some (like `++') are not universally supported \(em
305 see the shell manual page for details.
307 3.5\ Evaluation\ and\ quoting
309 The shell is a macro processor that
310 provides parameter substitution, command substitution and file
311 name generation for the arguments to commands.
312 This section discusses the order in which
313 these evaluations occur and the
314 effects of the various quoting mechanisms.
316 Commands are parsed initially according to the grammar
318 Before a command is executed
323 parameter substitution, e.g. \fB$user\fP
325 command substitution, e.g. \fB$(pwd)\fP or \fB\`pwd\`\fP
327 arithmetic expansion, e.g. \fB$(($count+1))\fP
330 Only one evaluation occurs so that if, for example, the value of the variable
332 is the string \fI$y\fP
337 will echo \fI$y\|.\fP
343 Following the above substitutions
344 the resulting characters
345 are broken into non-blank words (\fIblank interpretation\fP).
346 For this purpose `blanks' are the characters of the string
348 By default, this string consists of blank, tab and newline.
350 is not regarded as a word unless it is quoted.
355 will pass on the null string as the first argument to \fIecho\fP,
360 will call \fIecho\fR with no arguments
361 if the variable \fBnull\fP is not set
362 or set to the null string.
369 is then scanned for the file pattern characters
370 \fB\*(ST, ?\fR and \fB[\*(ZZ]\fR
371 and an alphabetical list of file names
372 is generated to replace the word.
373 Each such file name is a separate argument.
377 The evaluations just described also occur
378 in the list of words associated with a \fBfor\fP
380 Only substitution occurs
381 in the \fIword\fP used
382 for a \fBcase\fP branch.
384 As well as the quoting mechanisms described
385 earlier using \fB\\\fR and \fB\'\*(ZZ\'\fR
386 a third quoting mechanism is provided using double quotes.
387 Within double quotes parameter and command substitution
388 occurs but file name generation and the interpretation
390 The following characters
391 have a special meaning within double quotes
392 and may be quoted using \fB\\\|.\fP
394 \fB$ \fPparameter substitution
395 \fB$()\fP command substitution
396 \fB\`\fP command substitution
397 \fB"\fP ends the quoted string
398 \fB\e\fP quotes the special characters \fB$ \` " \e\fP
404 will pass the value of the variable \fBx\fP as a
405 single argument to \fIecho.\fP
410 will pass the positional parameters as a single
411 argument and is equivalent to
415 The notation \fB$@\fP
416 is the same as \fB$\*(ST\fR
417 except when it is quoted.
421 will pass the positional parameters, unevaluated, to \fIecho\fR
427 The following table gives, for each quoting mechanism,
428 the shell metacharacters that are evaluated.
447 Figure 2. Quoting mechanisms
451 In cases where more than one evaluation of a string
452 is required the built-in command \fIeval\fP
455 if the variable \fBX\fP has the value
456 \fI$y\fP, and if \fBy\fP has the value \fIpqr\fP
461 will echo the string \fIpqr\|.\fP
463 In general the \fIeval\fP command
464 evaluates its arguments (as do all commands)
465 and treats the result as input to the shell.
466 The input is read and the resulting command(s)
470 wg=\'eval who\*(VTgrep\'
478 \fIeval\fP is required
479 since there is no interpretation
480 of metacharacters, such as \fB\*(VT\|\fR, following
485 The treatment of errors detected by
486 the shell depends on the type of error
487 and on whether the shell is being
489 An interactive shell is one whose
490 input and output are connected
492 .\" Removed by Perry Metzger, obsolete and excess detail
494 .\" (as determined by
496 A shell invoked with the \fB\(mii\fP
497 flag is also interactive.
499 Execution of a command (see also 3.7) may fail
500 for any of the following reasons.
502 Input output redirection may fail.
503 For example, if a file does not exist
504 or cannot be created.
506 The command itself does not exist
507 or cannot be executed.
509 The command terminates abnormally,
510 for example, with a "bus error"
512 See Figure 2 below for a complete list
515 The command terminates normally
516 but returns a non-zero exit status.
518 In all of these cases the shell
519 will go on to execute the next command.
520 Except for the last case an error
521 message will be printed by the shell.
522 All remaining errors cause the shell
523 to exit from a script.
524 An interactive shell will return
525 to read another command from the terminal.
526 Such errors include the following.
529 e.g., if \*(ZZ then \*(ZZ done
531 A signal such as interrupt.
532 The shell waits for the current
533 command, if any, to finish execution and
534 then either exits or returns to the terminal.
536 Failure of any of the built-in commands
539 The shell flag \fB\(mie\fP
540 causes the shell to terminate
541 if any error is detected.
546 4* illegal instruction
550 8* floating point exception
551 9 kill (cannot be caught or ignored)
553 11* segmentation violation
554 12* bad argument to system call
555 13 write on a pipe with no one to read it
557 15 software termination (from \fIkill\fP (1))
562 Figure 3. UNIX signals\(dg
565 \(dg Additional signals have been added in modern Unix.
566 See \fIsigvec\fP(2) or \fIsignal\fP(3) for an up-to-date list.
568 Those signals marked with an asterisk
572 the shell itself ignores quit which is the only
573 external signal that can cause a dump.
574 The signals in this list of potential interest
575 to shell programs are 1, 2, 3, 14 and 15.
579 shell scripts normally terminate
580 when an interrupt is received from the
582 The \fItrap\fP command is used
583 if some cleaning up is required, such
584 as removing temporary files.
587 trap\ \'rm\ /tmp/ps$$; exit\'\ 2
589 sets a trap for signal 2 (terminal
590 interrupt), and if this signal is received
591 will execute the commands
596 another built-in command
597 that terminates execution of a shell script.
598 The \fIexit\fP is required; otherwise,
599 after the trap has been taken,
600 the shell will resume executing
602 at the place where it was interrupted.
604 UNIX signals can be handled in one of three ways.
605 They can be ignored, in which case
606 the signal is never sent to the process.
607 They can be caught, in which case the process
608 must decide what action to take when the
610 Lastly, they can be left to cause
611 termination of the process without
612 it having to take any further action.
613 If a signal is being ignored
614 on entry to the shell script, for example,
615 by invoking it in the background (see 3.7) then \fItrap\fP
616 commands (and the signal) are ignored.
618 The use of \fItrap\fP is illustrated
619 by this modified version of the \fItouch\fP
621 The cleanup action is to remove the file \fBjunk$$\fR\|.
626 trap\ \'rm\ \(mif\ junk$$;\ exit\'\ 1 2 3 15
629 \*(DC\(mic) flag=N ;;
630 \*(DC\*(ST) if\ test\ \(mif\ $i
631 \*(DC then cp\ $i\ junk$$;\ mv\ junk$$ $i
632 \*(DC elif\ test\ $flag
633 \*(DC then echo\ file\ \\'$i\\'\ does\ not\ exist
642 Figure 4. The touch command
645 The \fItrap\fP command
646 appears before the creation
647 of the temporary file;
648 otherwise it would be
649 possible for the process
650 to die without removing
653 Since there is no signal 0 in UNIX
654 it is used by the shell to indicate the
655 commands to be executed on exit from the
658 A script may, itself, elect to
659 ignore signals by specifying the null
660 string as the argument to trap.
661 The following fragment is taken from the
666 which causes \fIhangup, interrupt, quit \fRand\fI kill\fR
667 to be ignored both by the
668 script and by invoked commands.
670 Traps may be reset by saying
674 which resets the traps for signals 2 and 3 to their default values.
675 A list of the current values of traps may be obtained
681 The script \fIscan\fP (Figure 5) is an example
682 of the use of \fItrap\fP where there is no exit
684 \fIscan\fP takes each directory
685 in the current directory, prompts
686 with its name, and then executes
687 commands typed at the terminal
688 until an end of file or an interrupt is received.
689 Interrupts are ignored while executing
690 the requested commands but cause
691 termination when \fIscan\fP is
696 do\ if\ test\ \(mid\ $d/$i
698 \*(DO\*(THwhile\ echo\ "$i:"
699 \*(DO\*(TH\*(WHtrap\ exit\ 2
700 \*(DO\*(TH\*(WHread\ x
701 \*(DO\*(THdo\ trap\ :\ 2;\ eval\ $x;\ done
708 Figure 5. The scan command
711 \fIread x\fR is a built-in command that reads one line from the
713 and places the result in the variable \fBx\|.\fP
714 It returns a non-zero exit status if either
715 an end-of-file is read or an interrupt
718 3.8\ Command\ execution
720 To run a command (other than a built-in) the shell first creates
721 a new process using the system call \fIfork.\fP
722 The execution environment for the command
723 includes input, output and the states of signals, and
724 is established in the child process
725 before the command is executed.
726 The built-in command \fIexec\fP
727 is used in the rare cases when no fork
729 and simply replaces the shell with a new command.
730 For example, a simple version of the \fInohup\fP
736 The \fItrap\fP turns off the signals specified
737 so that they are ignored by subsequently created commands
738 and \fIexec\fP replaces the shell by the command
741 Most forms of input output redirection have already been
743 In the following \fIword\fP is only subject
744 to parameter and command substitution.
745 No file name generation or blank interpretation
746 takes place so that, for example,
750 will write its output into a file whose name is \fB\*(ST.c\|.\fP
751 Input output specifications are evaluated left to right
752 as they appear in the command.
754 The standard output (file descriptor 1)
755 is sent to the file \fIword\fP which is
756 created if it does not already exist.
757 .IP \*(AP\ \fIword\fP 12
758 The standard output is sent to file \fIword.\fP
759 If the file exists then output is appended
760 (by seeking to the end);
761 otherwise the file is created.
763 The standard input (file descriptor 0)
764 is taken from the file \fIword.\fP
765 .IP \*(HE\ \fIword\fP 12
766 The standard input is taken from the lines
767 of shell input that follow up to but not
768 including a line consisting only of \fIword.\fP
769 If \fIword\fP is quoted then no interpretation
770 of the document occurs.
771 If \fIword\fP is not quoted
772 then parameter and command substitution
773 occur and \fB\\\fP is used to quote
774 the characters \fB\\\fP \fB$\fP \fB\`\fP and the first character
776 In the latter case \fB\\newline\fP is ignored (c.f. quoted strings).
777 .IP >&\ \fIdigit\fP 12
778 The file descriptor \fIdigit\fP is duplicated using the system
780 and the result is used as the standard output.
781 .IP <&\ \fIdigit\fP 12
782 The standard input is duplicated from file descriptor \fIdigit.\fP
784 The standard input is closed.
786 The standard output is closed.
788 Any of the above may be preceded by a digit in which
789 case the file descriptor created is that specified by the digit
790 instead of the default 0 or 1.
795 runs a command with message output (file descriptor 2)
796 directed to \fIfile.\fP
800 runs a command with its standard output and message output
802 (Strictly speaking file descriptor 2 is created
803 by duplicating file descriptor 1 but the effect is usually to
804 merge the two streams.)
805 .\" Removed by Perry Metzger, most of this is now obsolete
808 .\" The environment for a command run in the background such as
810 .\" list \*(ST.c \*(VT lpr &
812 .\" is modified in two ways.
813 .\" Firstly, the default standard input
814 .\" for such a command is the empty file \fB/dev/null\|.\fR
815 .\" This prevents two processes (the shell and the command),
816 .\" which are running in parallel, from trying to
817 .\" read the same input.
818 .\" Chaos would ensue
819 .\" if this were not the case.
824 .\" would allow both the editor and the shell
825 .\" to read from the same input at the same time.
827 .\" The other modification to the environment of a background
828 .\" command is to turn off the QUIT and INTERRUPT signals
829 .\" so that they are ignored by the command.
830 .\" This allows these signals to be used
831 .\" at the terminal without causing background
832 .\" commands to terminate.
833 .\" For this reason the UNIX convention
834 .\" for a signal is that if it is set to 1
835 .\" (ignored) then it is never changed
836 .\" even for a short time.
837 .\" Note that the shell command \fItrap\fP
838 .\" has no effect for an ignored signal.
840 3.9\ Invoking\ the\ shell
842 The following flags are interpreted by the shell
844 If the first character of argument zero is a minus,
845 then commands are read from the file \fB.profile\|.\fP
846 .IP \fB\(mic\fP\ \fIstring\fP
848 If the \fB\(mic\fP flag is present then
849 commands are read from \fIstring\|.\fP
851 If the \fB\(mis\fP flag is present or if no
853 then commands are read from the standard input.
854 Shell output is written to
857 If the \fB\(mii\fP flag is present or
858 if the shell input and output are attached to a terminal (as told by \fIgtty\fP)
859 then this shell is \fIinteractive.\fP
860 In this case TERMINATE is ignored (so that \fBkill 0\fP
861 does not kill an interactive shell) and INTERRUPT is caught and ignored
862 (so that \fBwait\fP is interruptable).
863 In all cases QUIT is ignored by the shell.
867 When a command or pipeline (also known as a \fIjob\fP) is running in
868 the foreground, entering the stop character (typically
869 \s-1CONTROL-Z\s0 but user settable with the \fIstty\fP(1) command)
870 will usually cause the job to stop.
872 The jobs associated with the current shell may be listed by entering
873 the \fIjobs\fP command.
874 Each job has an associated \fIjob number\fP.
875 Jobs that are stopped may be continued by entering
879 and jobs may be moved to the foreground by entering
883 If there is a sole job with a particular name (say only one instance
884 of \fIcc\fP running), \fIfg\fP and \fIbg\fP may also use name of the
885 command in place of the number, as in:
889 If no `\fB%\fP' clause is entered, most recently stopped job
890 (indicated with a `+' by the \fIjobs\fP command) will be assumed.
891 See the manual page for the shell for more details.
895 The \fIalias\fP command creates a so-called shell alias, which is an
896 abbreviation that macro-expands at run time into some other command.
901 would cause the command sequence \fBls -F\fP to be executed whenever
902 the user types \fBls\fP into the shell.
903 Note that if the user types \fBls -a\fP, the shell will in fact
904 execute \fBls -F -a\fP.
905 The command \fBalias\fP on its own prints out all current aliases.
906 The \fIunalias\fP command, as in:
910 will remove an existing alias.
911 Aliases can shadow pre-existing commands, as in the example above.
912 They are typically used to override the interactive behavior of
913 commands in small ways, for example to always invoke some program with
914 a favorite option, and are almost never found in scripts.
916 3.12\ Command\ Line\ Editing\ and\ Recall
918 When working interactively with the shell, it is often tedious to
919 retype previously entered commands, especially if they are complicated.
920 The shell therefore maintains a so-called \fIhistory\fP, which is
921 stored in the file specified by the \fB\s-1HISTFILE\s0\fP environment
922 variable if it is set.
923 Users may view, edit, and re-enter previous lines of input using
924 a small subset of the commands of the \fIvi\fP(1) or
925 \fIemacs\fP(1)\(dg editors.
927 Technically, vi command editing is standardized by POSIX while emacs
929 However, all modern shells support both styles.
931 Emacs style editing may be selected by entering
935 and vi style editing may be selected with
939 The details of how command line editing works are beyond the scope of
941 See the shell manual page for details.
945 The design of the shell is
946 based in part on the original UNIX shell
948 unix command language thompson
950 and the PWB/UNIX shell,
952 pwb shell mashey unix
955 features having been taken from both.
956 Similarities also exist with the
958 of the Cambridge Multiple Access System
960 cambridge multiple access system hartley
967 I would like to thank Dennis Ritchie
968 and John Mashey for many
969 discussions during the design of the shell.
970 I am also grateful to the members of the Computing Science Research Center
971 and to Joe Maranzano for their
972 comments on drafts of this document.