date and time created 91/03/07 20:28:00 by bostic
[unix-history] / usr / src / bin / sh / sh.1
CommitLineData
65622797
KB
1.\" Copyright (c) 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Kenneth Almquist.
6.\"
7.\" %sccs.include.redist.man%
8.\"
9.\" @(#)sh.1 5.1 (Berkeley) %G%
10.\"
11.TH SH 1 ""
12.UC 7
13.de h \" subheading
14.sp
15.ti -0.3i
16.B "\\$1"
17.PP
18..
19.de d \" begin display
20.sp
21.in +4
22.nf
23..
24.de e \" end display
25.in -4
26.fi
27.sp
28..
29.de c \" command, etc.
30.br
31.HP 5
32\fB\\$1\fR
33.br
34..
35.de b \" begin builtin command
36.HP 5
37.B \\$1
38..
39.SH NAME
40ash \- a shell
41.SH SYNOPSIS
42.B ash
43[
44.B -efIijnsxz
45] [
46.B +efIijnsxz
47] [
48.B -c
49.I command
50] [
51.I arg
52] ...
53.SH COPYRIGHT
54Copyright 1989 by Kenneth Almquist.
55.SH DESCRIPTION
56.I Ash
57is a version of
58.I sh
59with features similar to those of the System V shell.
60This manual page lists all the features of
61.I ash
62but concentrates on the ones not in other shells.
63.h "Invocation"
64If the
65.B -c
66options is given, then the shell executes the specified shell command.
67The
68.B -s
69flag cause the shell to read commands from the standard input (after
70executing any command specified with the
71.B -c
72option.
73If neither the
74.B -s
75or
76.B -c
77options are set, then the first
78.I arg
79is taken as the name of a file to read commands from.
80If this is impossible because there are no arguments following
81the options, then
82.I ash
83will set the
84.B -s
85flag and will read commands from the standard input.
86.PP
87The shell sets the initial value of the positional parameters from the
88.IR arg s
89remaining after any
90.I arg
91used as the name of a file of commands is deleted.
92.PP
93The flags (other than
94.BR -c )
95are set by preceding them with ``-'' and cleared by preceding them
96with ``+''; see the
97.I set
98builtin command for a list of flags.
99If no value is specified for the
100.B -i
101flag, the
102.B -s
103flag is set, and the standard input and output of the shell
104are connected to terminals, then the
105.B -i
106flag will be set.
107If no value is specified for the
108.B -j
109flag, then the
110.B -j
111flag will be set if the
112.B -i
113flag is set.
114.PP
115When the shell is invoked with the
116.B -c
117option, it is good practice to include the
118.I -i
119flag if the command was entered interactively by a user.
120For compatibility with the System V shell, the
121.I -i
122option should come after the
123.B -c
124option.
125.PP
126If the first character of argument zero to the shell is ``-'',
127the shell is assumed to be a login shell, and the files
128.B /etc/profile
129and
130.B .profile
131are read if they exist.
132If the environment variable SHINIT is set on entry to the shell,
133the commands in SHINIT are normally parsed and executed. SHINIT is
134not examined if the shell is a login shell, or if it the shell is running a
135shell procedure. (A shell is considered to be running a shell
136procedure if neither the
137.B -s
138nor the
139.B -c
140options are set.)
141.h "Control Structures"
142A
143.I list
144is a sequence of zero or more commands separated by newlines,
145semicolons, or ampersands, and optionally terminated by one of these
146three characters. (This differs from the System V shell, which
147requires a list to contain at least one command in most cases.) The
148commands in a list are executed in the order they are written.
149If command is followed by an ampersand, the shell starts the command
150and immediately proceed onto the next command; otherwise it waits
151for the command to terminate before proceeding to the next one.
152.PP
153``&&'' and ``||'' are binary operators.
154``&&'' executes the first command, and then executes the second command
155iff the exit status of the first command is zero. ``||'' is similar,
156but executes the second command iff the exit status of the first command
157is nonzero. ``&&'' and ``||'' both have the same priority.
158.PP
159The ``|'' operator is a binary operator which feeds the standard output
160of the first command into the standard input of the second command.
161The exit status of the ``|'' operator is the exit status of the second
162command. ``|'' has a higher priority than ``||'' or ``&&''.
163.PP
164An
165.I if
166command looks like
167.d
168\fBif\fR list
169\fBthen\fR list
170.ti -\w'[ 'u
171[ \fBelif\fR list
172 \fBthen\fR list ] ...
173.ti -\w'[ 'u
174[ \fBelse\fR list ]
175\fBfi\fR
176.e
177.PP
178A
179.I while
180command looks like
181.d
182\fBwhile\fR list
183\fBdo\fR list
184\fBdone\fR
185.e
186The two lists are executed repeatedly while the exit status of the first
187list is zero. The
188.I until
189command is similar, but has the word
190.B until
191in place of
192.B while
193 repeats until the exit status of the first list
194is zero.
195.PP
196The
197.I for
198command looks like
199.d
200\fBfor\fR variable \fBin\fR word...
201\fBdo\fR list
202\fBdone\fR
203.e
204The words are expanded, and then the list is executed repeatedly with
205the variable set to each word in turn.
206.B do
207and
208.B done
209may be replaced with
210``{'' and ``}''.
211.PP
212The
213.I break
214and
215.I continue
216commands look like
217.d
218\fBbreak\fR [ num ]
219\fBcontinue\fR [ num ]
220.e
221.I Break
222terminates the
223.I num
224innermost
225.I for
226or
227.I while
228loops.
229.I Continue
230continues with the next iteration of the
231.IRnum'th
232innermost loop.
233These are implemented as builtin commands.
234.PP
235The
236.I case
237command looks like
238.d
239\fBcase\fR word \fBin\fR
240pattern\fB)\fR list \fB;;\fR
241\&...
242\fBesac\fR
243.e
244The pattern can actually be one or more patterns (see
245.I Patterns
246below), separated by ``|'' characters.
247.PP
248Commands may be grouped by writing either
249.d
250\fB(\fRlist\fB)\fR
251.e
252or
253.d
254\fB{\fR list; \fB}\fR
255.e
256The first of these executes the commands in a subshell.
257.PP
258A function definition looks like
259.d
260name \fB( )\fR command
261.e
262A function definition is an executable statement; when executed it installs
263a function named
264.B name
265and returns an exit status of zero.
266The command is normally a list enclosed between ``{'' and ``}''.
267.PP
268Variables may be declared to be local to a function by using a
269.I local
270command. This should appear as the first staement of a function,
271and looks like
272.d
273\fBlocal\fR [ variable | \fB-\fR ] ...
274.e
275.I Local
276is implemented as a builtin command.
277.PP
278When a variable is made local, it inherits the initial value and
279exported and readonly flags from the variable with the same name in the
280surrounding scope, if there is one. Otherwise, the variable is
281initially unset.
282.I Ash
283uses dynamic scoping, so that if you make the variable
284.B x
285local to function
286.IR f ,
287which then calls function
288.IR g ,
289references to the variable
290.B x
291made inside
292.I g
293will refer to the variable
294.B x
295declared inside
296.IR f ,
297not to the global variable named
298.BR x .
299.PP
300The only special parameter than can be made local is ``\fB-\fR''.
301Making ``\fB-\fR'' local any shell options that are changed via the
302.I set
303command inside the function to be restored to their original values
304when the function returns.
305.PP
306The
307.I return
308command looks like
309.d
310\fBreturn\fR [ exitstatus ]
311.e
312It terminates the currently executing function.
313.I Return
314is implemented as a builtin command.
315.h "Simple Commands"
316A simple command is a sequence of words. The execution of a simple
317command proceeds as follows. First, the leading words of the form
318``name=value'' are stripped off and assigned to the environment of
319the command. Second, the words are expanded. Third, the first
320remaining word is taken as the command name that command is located.
321Fourth, any redirections are performed. Fifth, the command is
322executed. We look at these operations in reverse order.
323.PP
324The execution of the command varies with the type of command.
325There are three types of commands: shell functions, builtin commands,
326and normal programs.
327.PP
328When a shell function is executed, all of the shell positional parameters
329(except $0, which remains unchanged) are set to the parameters to the shell
330function. The variables which are explicitly placed in the environment
331of the command (by placing assignments to them before the function name)
332are made local to the function and are set to values given.
333Then the command given in the function definition is executed.
334The positional parameters are restored to their original values when
335the command completes.
336.PP
337Shell builtins are executed internally to the shell, without spawning
338a new process.
339.PP
340When a normal program is executed, the shell runs the program, passing
341the parameters and the environment to the program. If the program is
342a shell procedure, the shell will interpret the program in a subshell.
343The shell will reinitialize itself in this case, so that the effect
344will be as if a new shell had been invoked to handle the shell procedure,
345except that the location of commands located in the parent shell will
346be remembered by the child. If the program is a file beginning with
347``#!'', the remainder of the first line specifies an interpreter for
348the program. The shell (or the operating system, under Berkeley UNIX)
349will run the interpreter in this case. The arguments to the interpreter
350will consist of any arguments given on the first line of the program,
351followed by the name of the program, followed by the arguments passed
352to the program.
353.h "Redirection"
354Input/output redirections can be intermixed with the words in a simple
355command and can be placed following any of the other commands. When
356redirection occurs, the shell saves the old values of the file descriptors
357and restores them when the command completes. The ``<'', ``>'', and ``>>''
358redirections open a file for input, output, and appending, respectively.
359The ``<&digit'' and ``>&digit'' makes the input or output a duplicate
360of the file descriptor numbered by the digit. If a minus sign is used
361in place of a digit, the standard input or standard output are closed.
362.PP
363The ``<<\ word'' redirection
364takes input from a
365.I here
366document.
367As the shell encounters ``<<'' redirections, it collects them. The
368next time it encounters an unescaped newline, it reads the documents
369in turn. The word following the ``<<'' specifies the contents of the
370line that terminates the document. If none of the quoting methods
371('', "", or \e) are used to enter the word, then the document is treated
372like a word inside double quotes: ``$'' and backquote are expanded
373and backslash can be used to escape these and to continue long lines.
374The word cannot contain any variable or command substitutions, and
375its length (after quoting) must be in the range of 1 to 79 characters.
376If ``<<-'' is used in place of ``<<'', then leading tabs are deleted
377from the lines of the document. (This is to allow you do indent shell
378procedures containing here documents in a natural fashion.)
379.PP
380Any of the preceding redirection operators may be preceded by a single
381digit specifying the file descriptor to be redirected. There cannot
382be any white space between the digit and the redirection operator.
383.h "Path Search"
384When locating a command, the shell first looks to see if it has a
385shell function by that name. Then, if PATH does not contain an
386entry for "%builtin", it looks for a builtin command by that name.
387Finally, it searches each entry in PATH in turn for the command.
388.PP
389The value of the PATH variable should be a series of entries separated
390by colons.
391Each entry consists of a directory name, or a directory name followed
392by a flag beginning with a percent sign.
393The current directory should be indicated by an empty directory name.
394.PP
395If no percent sign is present, then the entry causes the shell to
396search for the command in the specified directory. If the flag is
397``%builtin'' then the list of shell builtin commands is searched.
398If the flag is ``%func'' then the directory is searched for a file which
399is read as input to the shell. This file should define a function
400whose name is the name of the command being searched for.
401.PP
402Command names containing a slash are simply executed without performing
403any of the above searches.
404.h "The Environment"
405The environment of a command is a set of name/value pairs. When the
406shell is invoked, it reads these names and values, sets the shell
407variables with these names to the corresponding values, and marks
408the variables as exported. The
409.I export
410command can be used to mark additional variables as exported.
411.PP
412The environment of a command is constructed by constructing name/value
413pairs from all the exported shell variables, and then modifying this
414set by the assignments which precede the command, if any.
415.h "Expansion"
416The process of evaluating words when a shell procedure is executed is
417called
418.IR expansion .
419Expansion consists of four steps: variable substitution, command
420substitution, word splitting, and file name generation. If a word
421is the expression following the word
422.B case
423in a case statement, the file name
424which follows a redirection symbol, or an assignment to the environment
425of a command, then the word cannot be split into multiple words. In
426these cases, the last two steps of the expansion process are omitted.
427.h "Variable Substitution"
428To be written.
429.h "Command Substitution"
430.I Ash
431accepts two syntaxes for command substitution:
432.b
433`\fIlist\fR`
434.e
435and
436.b
437$(\fIlist\fR)
438.e
439Either of these may be included in a word.
440During the command substitution process, the command (syntactly a
441.IR list )
442will be executed and anything that the command writes to the standard
443output will be captured by the shell. The final newline (if any) of
444the output will be deleted; the rest of the output will be substituted
445for the command in the word.
446.h "Word Splitting"
447When the value of a variable or the output of a command is substituted,
448the resulting text is subject to word splitting, unless the dollar sign
449introducing the variable or backquotes containing the text were enclosed
450in double quotes. In addition, ``$@'' is subject to a special type of
451splitting, even in the presence of double quotes.
452.PP
453Ash uses two different splitting algorithms. The normal approach, which
454is intended for splitting text separated by which space, is used if the
455first character of the shell variable IFS is a space. Otherwise an alternative
456experimental algorithm, which is useful for splitting (possibly empty)
457fields separated by a separator character, is used.
458.PP
459When performing splitting, the shell scans the replacement text looking
460for a character (when IFS does not begin with a space) or a sequence of
461characters (when IFS does begin with a space), deletes the character or
462sequence of characters, and spits the word into two strings at that
463point. When IFS begins with a space, the shell deletes either of the
464strings if they are null. As a special case, if the word containing
465the replacement text is the null string, the word is deleted.
466.PP
467The variable ``$@'' is special in two ways. First, splitting takes
468place between the positional parameters, even if the text is enclosed
469in double quotes. Second, if the word containing the replacement
470text is the null string and there are no positional parameters, then
471the word is deleted. The result of these rules is that "$@" is
472equivalent to "$1" "$2" ... "$\fIn\fR", where \fIn\fR is the number of
473positional parameters. (Note that this differs from the System V shell.
474The System V documentation claims that "$@" behaves this way; in fact
475on the System V shell "$@" is equivalent to "" when there are no
476positional paramteters.)
477.h "File Name Generation"
478Unless the
479.B -f
480flag is set, file name generation is performed after word splitting is
481complete. Each word is viewed as a series of patterns, separated by
482slashes. The process of expansion replaces the word with the names of
483all existing files whose names can be formed by replacing each pattern
484with a string that matches the specified pattern. There are two
485restrictions on this: first, a pattern cannot match a string containing
486a slash, and second, a pattern cannot match a string starting with a
487period unless the first character of the pattern is a period.
488.PP
489If a word fails to match any files and the
490.B -z
491flag is not set, then the word will be left unchanged (except that the
492meta-characters will be converted to normal characters). If the
493.B -z
494flag is set, then the word is only left unchanged if none
495of the patterns contain a character that can match anything besides
496itself. Otherwise the
497.B -z
498flag forces the word to be replaced with the names of the files that it
499matches, even if there are zero names.
500.h "Patterns"
501A
502.I pattern
503consists of normal characters, which match themselves, and meta-characters.
504The meta-characters are ``!'', ``*'', ``?'', and ``[''. These characters lose
505there special meanings if they are quoted. When command or variable
506substitution is performed and the dollar sign or back quotes are not
507double quoted, the value of the variable or the output of the command
508is scanned for these characters and they are turned into meta-characters.
509.PP
510Two exclamation points at the beginning of a pattern function as a ``not''
511operator, causing the pattern to match any string that the remainder of
512the pattern does
513.I not
514match. Other occurances of exclamation points in a pattern match
515exclamation points. Two exclamation points are required rather than one
516to decrease the incompatibility with the System V shell (which does not
517treat exclamation points specially).
518.PP
519An asterisk (``*'') matches any string of characters.
520A question mark matches any single character.
521A left bracket (``['') introduces a character class. The end of the
522character class is indicated by a ``]''; if the ``]'' is missing then
523the ``['' matches a ``['' rather than introducing a character class.
524A character class matches any of the characters between the square
525brackets. A range of characters may be specified using a minus sign.
526The character class may be complemented by making an exclamation point
527the first character of the character class.
528.PP
529To include a ``]'' in a character class, make it the first character listed
530(after the ``!'', if any).
531To include a minus sign, make it the first or last character listed.
532.h "The /u Directory"
533By convention, the name ``/u/user'' refers to the home directory of the
534specified user. There are good reasons why this feature should be supported
535by the file system (using a feature such as symbolic links) rather than
536by the shell, but
537.I ash
538is capable of performing this mapping if the file system doesn't.
539If the mapping is done by
540.IR ash ,
541setting the
542.B -f
543flag will turn it off.
544.h "Character Set"
545.I Ash
546silently discards nul characters. Any other character will be handled
547correctly by
548.IR ash ,
549including characters with the high order bit set.
550.h "Job Names and Job Control"
551The term
552.I job
553refers to a process created by a shell command, or in the case of a
554pipeline, to the set of processes in the pipeline. The ways to refer
555to a job are:
556.b
557%\fInumber\fR
558%\fIstring\fR
559%%
560\fIprocess_id\fR
561.e
562The first form identifies a job by job number.
563When a command is run,
564.I ash
565assigns it a job number
566(the lowest unused number is assigned).
567The second form identifies a job by giving a prefix of the command used
568to create the job. The prefix must be unique. If there is only one job,
569then the null prefix will identify the job, so you can refer to the job
570by writing ``%''. The third form refers to the \fIcurrent job\fR. The
571current job is the last job to be stopped while it was in the foreground.
572(See the next paragraph.) The last form identifies a job by giving the
573process id of the last process in the job.
574.PP
575If the operating system that
576.I ash
577is running on supports job control,
578.I ash
579will allow you to use it.
580In this case, typing the suspend character (typically ^Z) while running
581a command will return you to
582.I ash
583and will make the suspended command the current job. You can then continue
584the job in the background by typing
585.IR bg ,
586or you can continue it in the foreground by typing
587.IR fg .
588.h "Atty"
589If the shell variable ATTY is set, and the shell variable TERM is not
590set to ``emacs'', then \fIash\fR generates appropriate escape sequences
591to talk to
592.IR atty (1).
593.h "Exit Statuses"
594By tradition, an exit status of zero means that a command has succeeded
595and a nonzero exit status indicates that the command failed. This is
596better than no convention at all, but in practice it is extremely useful
597to allow commands that succeed to use the exit status to return information
598to the caller. A variety of better conventions have been proposed, but
599none of them has met with universal approval. The convention used by
600\fIash\fR and all the programs included in the \fIash\fR distribution is
601as follows:
602.ta 1i,2i
603.nf
604 0 Success.
605 1 Alternate success.
606 2 Failure.
607 129-... Command terminated by a signal.
608.fi
609The \fIalternate success\fR return is used by commands to indicate various
610conditions which are not errors but which can, with a little imagination,
611be conceived of as less successful than plain success. For example,
612.I test
613returns 1 when the tested condition is false and
614.I getopts
615returns 1 when there are no more options.
616Because this convention is not used universally, the
617.B -e
618option of
619.I ash
620causes the shell to exit when a command returns 1 even though that
621contradicts the convention described here.
622.PP
623When a command is terminated by a signal, the uses 128 plus the signal
624number as the exit code for the command.
625.h "Builtin Commands"
626This concluding section lists the builtin commands which are builtin
627because they need to perform some operation that can't be performed by a
628separate process. In addition to these, there are several other commands
629.RI ( catf ,
630.IR echo ,
631.IR expr ,
632.IR line ,
633.IR nlecho ,
634.IR test ,
635.RI `` : '',
636and
637.IR true )
638which can optionally be compiled into the shell. The builtin
639commands described below that accept options use the System V Release 2
640.IR getopt (3)
641syntax.
642.sp
643.b bg
644[
645.I job
646] ...
647.br
648Continue the specified jobs (or the current job if no jobs are given)
649in the background.
650This command is only available on systems with Bekeley job control.
651.b bltin
652.IR "command arg" ...
653.br
654Execute the specified builtin command. (This is useful when you have a
655shell function with the same name as a builtin command.)
656.b cd
657[
658.I directory
659]
660.br
661Switch to the specified directory (default $HOME).
662If the an entry for CDPATH appears in the environment of the cd command
663or the shell variable CDPATH is set and the directory name does not
664begin with a slash, then the directories listed in CDPATH will be
665searched for the specified directory. The format of CDPATH is the
666same as that of PATH.
667In an interactive shell, the cd command will print out the name of the
668directory that it actually switched to if this is different from the
669name that the user gave. These may be different either because
670the CDPATH mechanism was used or because a symbolic link was crossed.
671.b ".\fI\h'0.1i'file"
672.br
673The commands in the specified file are read and executed by the shell.
674A path search is not done to find the file because the directories in
675PATH generally contain files that are intended to be executed, not read.
676.b eval
677.IR string ...
678.br
679The strings are parsed as shell commands and executed.
680(This differs from the System V shell, which concatenates the arguments
681(separated by spaces) and parses the result as a single command.)
682.b exec
683[
684.IR "command arg" ...
685]
686.br
687Unless
688.I command
689is omitted,
690the shell process is replaced with the specified program (which must be a real
691program, not a shell builtin or function).
692Any redirections on the exec command are marked as permanent, so that they
693are not undone when the exec command finishes.
694If the command is not found, the exec command causes the shell to exit.
695.b exit
696[
697.I exitstatus
698]
699.br
700Terminate the shell process. If
701.I exitstatus
702is given it is used as the
703exit status of the shell; otherwise the exit status of the preceding
704command is used.
705.b export
706.IR name ...
707.br
708The specified names are exported so that they will appear in the environment
709of subsequent commands. The only way to un-export a variable is to unset it.
710.I Ash
711allows the value of a variable to be set at the same time it is exported
712by writing
713.d
714\fBexport\fR name=value
715.e
716With no arguments the export command lists the names of all exported variables.
717.b fg
718[
719.I job
720]
721.br
722Move the specified job or the current job to the foreground.
723This command is only available on systems with Bekeley job control.
724.b getopts
725.I optstring
726.I var
727.br
728The System V
729.I getopts
730command.
731.b hash
732.B -rv
733.IR command ...
734.br
735The shell maintains a hash table which remembers the locations of
736commands. With no arguments whatsoever, the hash command prints
737out the contents of this table. Entries which have not been looked
738at since the last
739.I cd
740command are marked with an asterisk; it is possible for these entries
741to be invalid.
742.sp
743With arguments, the hash command removes the specified commands from
744the hash table (unless they are functions) and then locates them.
745With the
746.B -v
747option,
748.I hash
749prints the locations of the commands as it finds them.
750The
751.B -r
752option causes the
753.I hash
754command to delete all the entries in the hash table except for
755functions.
756.b jobid
757[
758.I job
759]
760.br
761Print the process id's of the processes in the job. If the job argument
762is omitted, use the current job.
763.b jobs
764.br
765This command lists out all the background processes which are children
766of the current shell process.
767.b lc
768[
769.I function-name
770]
771.br
772The function name is defined to execute the last command entered.
773If the function name is omitted, the last command executed is
774executed again. This command only works if the
775.B -i
776flag is set.
777.b pwd
778.br
779Print the current directory. The builtin command may differ from the
780program of the same name because the builtin command remembers what
781the current directory is rather than recomputing it each time. This
782makes it faster. However, if the current directory is renamed, the
783builtin version of pwd will continue to print the old name for the
784directory.
785.b read
786[
787.B -p
788.I prompt
789]
790[
791.B -e
792]
793.IR variable ...
794.br
795The prompt is printed if the
796.B -p
797option is specified and the standard input is a terminal. Then a
798line is read from the standard input. The trailing newline is deleted
799from the line and the line is split as described
800in the section on word splitting above, and the pieces are assigned to
801the variables in order. If there are more pieces than variables, the
802remaining pieces (along with the characters in IFS that separated them)
803are assigned to the last variable. If there are more variables than
804pieces, the remaining variables are assigned the null string.
805.sp
806The
807.B -e
808option causes any backslashes in the input to be treated specially.
809If a backslash is followed by a newline, the backslash and the newline
810will be deleted. If a backslash is followed by any other character,
811the backslash will be deleted and the following character will be treated
812as though it were not in IFS, even if it is.
813.b readonly
814.IR name ...
815.br
816The specified names are marked as read only, so that they cannot be
817subsequently modified or unset.
818.I Ash
819allows the value of a variable to be set at the same time it is marked
820read only by writing
821.d
822\fBreadonly\fR name=value
823.e
824With no arguments the readonly command lists the names of all
825read only variables.
826.b set
827[
828{
829.BI - options
830|
831.BI + options
832|
833.B --
834}
835]
836.IR arg ...
837.br
838The
839.I set
840command performs three different functions.
841.sp
842With no arguments, it lists the values of all shell variables.
843.sp
844If options are given, it sets the specified option flags, or clears
845them if the option flags are introduced with a
846.B +
847rather than a
848.BR - .
849Only the first argument to
850.I set
851can contain options.
852The possible options are:
853.sp
854.ta 0.4i
855.in +0.4i
856.ti -0.4i
857\fB-e\fR Causes the shell to exit when a command terminates with
858a nonzero exit status, except when the exit status of the command is
859explicitly tested. The exit status of a command is considered to be
860explicitly tested if the command is used to control an
861.IR if ,
862.IR elif ,
863.IR while ,
864or
865.IR until ;
866or if the command is the left hand operand of an ``&&'' or ``||''
867operator.
868.sp
869.ti -0.4i
870\fB-f\fR Turn off file name generation.
871.sp
872.ti -0.4i
873\fB-I\fR Cause the shell to ignore end of file conditions.
874(This doesn't apply when the shell a script sourced using the ``.''
875command.) The shell will in fact exit if it gets 50 eof's in a
876row.
877.sp
878.ti -0.4i
879\fB-i\fR Make the shell interactive. This causes the shell to
880prompt for input, to trap interrupts, to ignore quit and terminate signals,
881and to return to the main command loop rather than exiting on error.
882.sp
883.ti -0.4i
884\fB-j\fR Turns on Berkeley job control, on systems that support it.
885When the shell starts up, the
886.B -j
887is set by default if the
888.B -i
889flag is set.
890.sp
891.ti -0.4i
892\fB-n\fR Causes the shell to read commands but not execute them.
893(This is marginally useful for checking the syntax of scripts.)
894.sp
895.ti -0.4i
896\fB-s\fR If this flag is set when the shell starts up, the shell
897reads commands from its standard input. The shell doesn't examine the
898value of this flag any other time.
899.sp
900.ti -0.4i
901\fB-x\fR If this flag is set, the shell will print out each
902command before executing it.
903.sp
904.ti -0.4i
905\fB-z\fR If this flag is set, the file name generation process
906may generate zero files. If it is not set, then a pattern which does
907not match any files will be replaced by a quoted version of the pattern.
908.in -0.4i
909.sp
910The third use of the set command is to set the values of the shell's
911positional parameters to the specified
912.IR args .
913To change the positional parameters without changing any options,
914use ``\fB--\fR'' as the first argument to
915.IR set .
916If no args are present, the set command will leave the value of the
917positional parameters unchanged, so to set the positional parameters
918to set of values that may be empty, execute the command
919.d
920shift $#
921.e
922first to clear out the old values of the positional parameters.
923.b setvar
924.I variable
925.I value
926.br
927Assigns
928.I value
929to
930.IR variable .
931(In general it is better to write
932.I variable=value
933rather than using
934.IR setvar .
935.I Setvar
936is intended to be used in functions that assign values to variables whose
937names are passed as parameters.)
938.b shift
939[
940.I n
941]
942.br
943Shift the positional parameters
944.I n
945times.
946A shift sets the value of $1 to the value of $2, the value of $2 to
947the value of $3, and so on, decreasing the value of $# by one.
948If there are zero positional parameters, shifting doesn't do anything.
949.b trap
950[
951.I action
952]
953.IR signal ...
954.br
955Cause the shell to parse and execute
956.I action
957when any of the specified signals are received.
958The signals are specified by signal number.
959.I Action
960may be null or omitted;
961the former causes the specified signal to be ignored and the latter
962causes the default action to be taken.
963When the shell forks off a subshell, it resets trapped (but not ignored)
964signals to the default action.
965The trap command has no effect on signals that were ignored on entry
966to the shell.
967.b umask
968[
969.I mask
970]
971.br
972Set the value of umask (see
973.IR umask (2))
974to the specified octal value. If the argument is omitted, the umask
975value is printed.
976.b unset
977.IR name ...
978.br
979The specified variables and functions are unset and unexported.
980If a given name corresponds to both a variable and a function, both the
981variable and the function are unset.
982.b wait
983[
984.I job
985]
986.br
987Wait for the specified job to complete and return the exit status of the
988last process in the job. If the argument is omitted, wait for all jobs
989to complete and the return an exit status of zero.
990.SH EXAMPLES
991The following function redefines the \fIcd\fR command:
992.d
993cd() {
994 if bltin cd "$@"
995 then if test -f .enter
996 then . .enter
997 else return 0
998 fi
999 fi
1000}
1001.e
1002This function causes the file ``.enter'' to be read when you enter a
1003directory, if it exists. The \fIbltin\fR command is used to access the
1004real \fIcd\fR command. The ``return 0'' ensures that the function will
1005return an exit status of zero if it successfully changes to a directory
1006that does not contain a ``.enter'' file. Redefining existing commands
1007is not always a good idea, but this example shows that you can do it if
1008you want to.
1009.PP
1010The suspend function distributed with
1011.I ash
1012looks like
1013.d
1014# Copyright (C) 1989 by Kenneth Almquist. All rights reserved.
1015# This file is part of ash, which is distributed under the terms
1016# specified by the Ash General Public License.
1017
1018suspend() {
1019 local -
1020 set +j
1021 kill -TSTP 0
1022}
1023.e
1024This turns off job control and then sends a stop signal to the current
1025process group, which suspends the shell. (When job control is turned
1026on, the shell ignores the TSTP signal.) Job control will be turned back
1027on when the function returns because ``-'' is local to the function.
1028As an example of what \fInot\fR to do, consider an earlier version of
1029\fIsuspend\fR:
1030.d
1031suspend() {
1032 suspend_flag=$-
1033 set +j
1034 kill -TSTP 0
1035 set -$suspend_flag
1036}
1037.e
1038There are two problems with this. First, \fBsuspend_flag\fR is a global
1039variable rather than a local one, which will cause problems in the
1040(unlikely) circumstance that the user is using that variable for some
1041other purpose. Second, consider what happens if shell received an interrupt
1042signal after it executes the first \fIset\fR command but before it executes
1043the second one. The interrupt signal will abort the shell function, so
1044that the second \fIset\fR command will never be executed and job control
1045will be left off. The first version of \fIsuspend\fR avoids this problem
1046by turning job control off only in a local copy of the shell options. The
1047local copy of the shell options is discarded when the function is terminated,
1048no matter how it is terminated.
1049.SH HINTS
1050Shell variables can be used to provide abbreviations for things which
1051you type frequently. For example, I set
1052.br
1053\h'1i'export h=$HOME
1054.br
1055in my .profile so that I can type the name of my home directory simply
1056by typing ``$h''.
1057.PP
1058When writing shell procedures, try not to make assumptions about what is
1059imported from the environment. Explicitly unset or initialize all variables,
1060rather than assuming they will be unset. If you use cd, it is a good idea
1061to unset CDPATH.
1062.PP
1063People sometimes use ``<&-'' or ``>&-'' to provide no input to a command
1064or to discard the output of a command. A better way to do this is
1065to redirect the input or output of the command to
1066.BR /dev/null .
1067.PP
1068Word splitting and file name generation are performed by default,
1069and you have to explicitly use double quotes to suppress it. This is
1070backwards, but you can learn to live with it. Just get in the habit of
1071writing double quotes around variable and command substitutions, and
1072omit them only when you really want word splitting and file name generation.
1073If you want word splitting but not file name generation, use the
1074.B -f
1075option.
1076.SH AUTHORS
1077Kenneth Almquist
1078.SH "SEE ALSO"
1079echo(1), expr(1), line(1), pwd(1), true(1).
1080.SH BUGS
1081When command substitution occurs inside a here document, the commands inside
1082the here document are run with their standard input closed. For example,
1083the following will not word because the standard input of the
1084.I line
1085command will be closed when the command is run:
1086.d
1087cat <<-!
1088Line 1: $(line)
1089Line 2: $(line)
1090!
1091.e
1092.PP
1093Unsetting a function which is currently being executed may cause strange
1094behavior.
1095.PP
1096The shell syntax allows a here document to be terminated by an end of file
1097as well as by a line containing the terminator word which follows the ``<<''.
1098What this means is that if you mistype the terminator line, the shell
1099will silently swallow up the rest of your shell script and stick it
1100in the here document.