xref: /csrg-svn/old/sh/sh.1 (revision 50827)
144281Scael.\" Copyright (c) 1990 Regents of the University of California.
2*50827Scael.\" All rights reserved.
319463Smckusick.\"
4*50827Scael.\" %sccs.include.proprietary.roff%
543084Scael.\"
6*50827Scael.\"	@(#)sh.1	6.8 (Berkeley) 08/09/91
7*50827Scael.\"
843084Scael.Dd
943084Scael.Dt SH 1
1043084Scael.Os ATT 7th
1143084Scael.Sh NAME
1243084Scael.Nm sh
1343084Scael.Nd shell command interpreter
1443084Scael.Sh SYNOPSIS
1543084Scael.Nm sh
1643084Scael.Op Fl ceiknrstuvx
1743084Scael.Op arg
1843084Scael.Ar ...
1943084Scael.Sh DESCRIPTION
2043084Scael.Nm Sh
2119464Smckusickis a command programming language that executes commands read from a terminal
2243084Scaelor a file. The shell this page describes is called the
2343084Scael.Em Bourne
2443084Scaelshell.
2543084Scael.Pp
2643084ScaelCommand line options:
2743084Scael.Pp
2846276SkarelsIf the first character of argument 0 is
29*50827Scael.Fl ,
3043084Scaelcommands are read from
3143084Scael.Pa $HOME/.profile ,
3243084Scaelif such a file exists.
3343084ScaelThe following flags are interpreted by the shell when it is invoked.
34*50827Scael.Bl -tag -width flag
35*50827Scael.It Fl c Ar string
3643084ScaelCommands are read from
3743084Scael.Ar string.
38*50827Scael.It Fl s
3943084ScaelIf the
4043084Scael.Fl s
4143084Scaelflag is present or if no arguments remain
4243084Scaelthen commands are read from the standard input.
4343084ScaelShell output is written to file descriptor 2.
44*50827Scael.It Fl i
4543084ScaelIf the
4643084Scael.Fl i
4743084Scaelflag is present or
4843084Scaelif the shell input and output are attached to a terminal (as told by
4943084Scael.Xr getty  )
5043084Scaelthen this shell is
5143084Scael.Em interactive .
52*50827ScaelIn this case the terminate signal
53*50827Scael.Dv SIGTERM
54*50827Scael(see
5543084Scael.Xr sigvec  2  )
5643084Scaelis ignored (so that 'kill 0'
5743084Scaeldoes not kill an interactive shell) and the interrupt signal
58*50827Scael.Dv SIGINT
59*50827Scaelis caught and ignored (so that
6043084Scael.Xr wait
6143084Scaelis interruptible).
62*50827ScaelIn all cases
63*50827Scael.Dv SIGQUIT
64*50827Scaelis ignored by the shell.
65*50827Scael.El
6643084Scael.Pp
6743084ScaelThis next set of options can be used on the command line invoking
6843084Scaelthe
6943084Scael.Nm sh
7043084Scaelor set with the interactive command
7143084Scael.Ic set .
72*50827Scael.Bl -tag -width Fl e
73*50827Scael.It Fl e
7443084ScaelIf non interactive, exit immediately if a command fails.
75*50827Scael.It Fl k
7643084ScaelAll keyword arguments are placed in the environment for a command,
7743084Scaelnot just those that precede the command name.
78*50827Scael.It Fl n
7943084ScaelRead commands but do not execute them.
80*50827Scael.It Fl t
8143084ScaelExit after reading and executing one command.
82*50827Scael.It Fl u
8343084ScaelTreat unset variables as an error when substituting.
84*50827Scael.It Fl v
8543084ScaelPrint shell input lines as they are read.
86*50827Scael.It Fl x
8743084ScaelPrint commands and their arguments as they are executed.
88*50827Scael.It Fl
8943084ScaelTurn off the
9043084Scael.Fl x
9143084Scaeland
9243084Scael.Fl v
9343084Scaeloptions.
94*50827Scael.El
95*50827Scael.Ss Commands
9619463SmckusickA
9743084Scael.Em simple-command
9819463Smckusickis a sequence of non blank
9943084Scael.Em words
10019463Smckusickseparated by blanks (a blank is a
10143084Scael.Em tab
10219463Smckusickor a
10343084Scael.Em space  ) .
10419464SmckusickThe first word specifies the name of the command to be executed.
10519464SmckusickExcept as specified below the remaining words are passed as arguments
10619463Smckusickto the invoked command.
10719464SmckusickThe command name is passed as argument 0 (see
10843084Scael.Xr execve  2  ) .
10919463SmckusickThe
11043084Scael.Em value
11119463Smckusickof a simple-command is its exit status
11243084Scaelif it terminates normally or
113*50827Scael.Li 200+ Ns Ns Em status
11443084Scaelif it terminates abnormally (see
11543084Scael.Ar sigvec  2
11619464Smckusickfor a list of status values).
11743084Scael.Pp
11819463SmckusickA
11943084Scael.Em pipeline
12019463Smckusickis a sequence of one or more
12143084Scael.Em commands
12219463Smckusickseparated by
12343084Scael.Sq Nm \&|  .
12419464SmckusickThe standard output of each command but the last is connected by a
12543084Scael.Xr pipe  2
12619463Smckusickto the standard input of the next command.
12719463SmckusickEach command is run as a separate process;
12819463Smckusickthe shell waits for the last command to terminate.
12943084Scael.Pp
13019463SmckusickA
13143084Scael.Em list
13219463Smckusickis a sequence of one or more
13343084Scael.Em pipelines
13419463Smckusickseparated by
13543084Scael.Nm \&; ,
13643084Scael.Nm \&& ,
13743084Scael.Nm \&&&
13819463Smckusickor
13943084Scael.Nm \&|
14043084Scaelor
14143084Scael.Nm \&|\&|
14219463Smckusickand optionally terminated by
14343084Scael.Nm \&;
14419463Smckusickor
14543084Scael.Nm \&& .
14643084Scael.Nm \&;
14719463Smckusickand
14843084Scael.Nm \&&
14919464Smckusickhave equal precedence which is lower than that of
15043084Scael.Nm \&&&
15119463Smckusickand
15243084Scael.Nm \&|\&| ,
15343084Scael.Nm &&
15419463Smckusickand
15543084Scael.Nm \&|\&| ,
15619463Smckusickalso have equal precedence.
15719464SmckusickA semicolon causes sequential execution; an ampersand causes the preceding
15843084Scael.Em pipeline
15919464Smckusickto be executed without waiting for it to finish.  The symbol
16043084Scael.Nm \&&&
16143084Scael.Pq Nm \&|\&|
16219463Smckusickcauses the
16343084Scael.Em list
16419463Smckusickfollowing to be executed only if the preceding
16543084Scael.Em pipeline
16619464Smckusickreturns a zero (non zero) value.  Newlines may appear in a
16743084Scael.Em list ,
16819464Smckusickinstead of semicolons, to delimit commands.
16943084Scael.Pp
17019463SmckusickA
17143084Scael.Em command
17219464Smckusickis either a simple-command or one of the following.
17319463SmckusickThe value returned by a command is that of the
17419463Smckusicklast simple-command executed in the command.
175*50827Scael.Bl -tag -width Fl
176*50827Scael.It Xo
177*50827Scael.Ic for Ar name
178*50827Scael.Op Ic \&in Ar word ...
179*50827Scael.Ic \&do Ar list Ic done
180*50827Scael.Xc
18119463SmckusickEach time a
18243084Scael.Ic for
18319463Smckusickcommand is executed
18443084Scael.Ar name
18519463Smckusickis set to the next word in the
18643084Scael.Ic for
18727208Slepreauword list.
18819463SmckusickIf
189*50827Scael.Ic \&in
19043084Scael.Ar word \&...
19119464Smckusickis omitted,
192*50827Scael.Ic \&in
19343084Scael.Dq Ic \&$@
194*50827Scaelis assumed.
195*50827ScaelExecution ends when there are no more words in the list.
196*50827Scael.It Xo
197*50827Scael.Ic case Ar word
198*50827Scael.Ic \&in
199*50827Scael.Oo Ar pattern Op Ar \&| pattern
200*50827Scael.Ar ... Ic \&) Ar list Ic \&;; Oc
201*50827Scael.Ar ... Ar esac
202*50827Scael.Xc
20319463SmckusickA
20443084Scael.Ic case
20519463Smckusickcommand executes the
20643084Scael.Ar list
20719464Smckusickassociated with the first pattern that matches
20843084Scael.Ar word .
20919464SmckusickThe form of the patterns is the same as that used for file name generation.
210*50827Scael.It Xo
211*50827Scael.Ic \&if Ar list
212*50827Scael.Ic then Ar list
21343084Scael.Op Ic elif Ar list Ic then Ar list
214*50827Scael.Ar ...
215*50827Scael.Op Ic else Ar list Ic
216*50827Scael.No \&fi
217*50827Scael.Xc
21819463SmckusickThe
21943084Scael.Ar list
22019463Smckusickfollowing
221*50827Scael.Ic \&if
22219463Smckusickis executed and if it returns zero the
22343084Scael.Ar list
22419463Smckusickfollowing
22543084Scael.Ic then
22619464Smckusickis executed.  Otherwise, the
22743084Scael.Ar list
22819463Smckusickfollowing
22943084Scael.Ic elif
23019464Smckusickis executed and if its value is zero the
23143084Scael.Ar list
23219463Smckusickfollowing
23343084Scael.Ic then
23419464Smckusickis executed.  Failing that the
23543084Scael.Ic else
23643084Scael.Ar list
23719463Smckusickis executed.
238*50827Scael.It Xo
239*50827Scael.Ic while Ar list
240*50827Scael.Op Ic \&do Ar list
241*50827Scael.Ic done
242*50827Scael.Xc
24319463SmckusickA
24443084Scael.Ic while
24519463Smckusickcommand repeatedly executes the
24643084Scael.Ic  while
24743084Scael.Ar list
24819463Smckusickand if its value is zero executes the
249*50827Scael.Ic \&do
25043084Scael.Ar list ;
25119464Smckusickotherwise the loop terminates.  The value returned by a
25243084Scael.Ic while
25319464Smckusickcommand is that of the last executed command in the
254*50827Scael.Ic \&do
25543084Scael.Ar list .
25643084Scael.Ic until
25719463Smckusickmay be used in place of
25843084Scael.Ic while
25919464Smckusickto negate the loop termination test.
260*50827Scael.It Ic \&( Ar list Ic \&)
26119463SmckusickExecute
26243084Scael.Ar list
26319463Smckusickin a subshell.
264*50827Scael.It Ic \&{ Ar list Ic \&}
26543084Scael.Ar list
26619463Smckusickis simply executed.
267*50827Scael.El
26843084Scael.Pp
26919464SmckusickThe following words are only recognized as the first word of a command
27019463Smckusickand when not quoted.
271*50827Scael.Bd -filled -offset indent -compact
27243084Scael.Nm if then else elif fi case in esac
27343084Scael.Nm for while until do done \&{ \&}
274*50827Scael.Ed
27543084Scael.Ss Command substitution
27643084ScaelThe standard output from a command enclosed in a pair of back quotes
27743084Scael.Pq Ic \&``
27819464Smckusickmay be used as part or all of a word; trailing newlines are removed.
279*50827Scael.Ss Parameter substitution
28019463SmckusickThe character
28143084Scael.Ic \&$
28219464Smckusickis used to introduce substitutable parameters.
28319463SmckusickPositional parameters may be assigned values by
28443084Scael.Ic set  .
28519463SmckusickVariables may be set by writing
28643084Scael.Pp
287*50827Scael.Bd -filled -offset indent -compact
28843084Scael.Ar name=value
28943084Scael.Op Ar name=value
29043084Scael\&...
291*50827Scael.Ed
29243084Scael.Pp
293*50827Scael.Bl -tag -width flag
294*50827Scael.It Ic \&$\&{ Ns Ar parameter Ns Ic \&}
29519463SmckusickA
29643084Scael.Ar parameter
29719463Smckusickis a sequence of letters, digits or underscores (a
29843084Scael.Ar name  ) ,
29919464Smckusicka digit, or any of the characters
300*50827Scael.Nm "\&* \&@ \&# \&? \&\- \&$ \&!\ " .
30119463SmckusickThe value, if any, of the parameter is substituted.
30219463SmckusickThe braces are required only when
30343084Scael.Ar parameter
30419463Smckusickis followed by a letter, digit, or underscore
30519464Smckusickthat is not to be interpreted as part of its name.  If
30643084Scael.Ar parameter
30719464Smckusickis a digit, it is a positional parameter.  If
30843084Scael.Ar parameter
30919463Smckusickis
31043084Scael.Ic \&*
31143084Scaelor
31243084Scael.Ic \&@
31319464Smckusickthen all the positional parameters, starting with
31443084Scael.Ic $1 ,
31519464Smckusickare substituted separated by spaces.
31643084Scael.Ic $0
31719464Smckusickis set from argument zero when the shell is invoked.
318*50827Scael.Sm off
319*50827Scael.It Xo
320*50827Scael.Ic \&$\&{ Ar parameter
321*50827Scael.Ic - Ar word Ic \&}
322*50827Scael.Xc
323*50827Scael.Sm on
32419463SmckusickIf
32543084Scael.Ar parameter
32619464Smckusickis set, substitute its value; otherwise substitute
32743084Scael.Ar word .
328*50827Scael.Sm off
329*50827Scael.It Xo
330*50827Scael.Ic \&$\&{ Ar parameter
331*50827Scael.Ic \&= Ar word Ic \&}
332*50827Scael.Xc
333*50827Scael.Sm on
33419463SmckusickIf
33543084Scael.Ar parameter
33619464Smckusickis not set, set it to
33743084Scael.Ar word ;
33819463Smckusickthe value of the parameter is then substituted.
33919464SmckusickPositional parameters may not be assigned to in this way.
340*50827Scael.Sm off
341*50827Scael.It Xo
342*50827Scael.Ic \&$\&{ Ar parameter
343*50827Scael.Ic \&? Ar word Ic \&}
344*50827Scael.Xc
345*50827Scael.Sm on
34619463SmckusickIf
34743084Scael.Ar parameter
34819464Smckusickis set, substitute its value; otherwise, print
34943084Scael.Ar word
35019464Smckusickand exit from the shell.  If
35143084Scael.Ar word
35219464Smckusickis omitted, a standard message is printed.
353*50827Scael.Sm off
354*50827Scael.It Xo
355*50827Scael.Ic \&$\&{ Ar parameter
356*50827Scael.Ic \&+ Ar word Ic \&}
357*50827Scael.Xc
358*50827Scael.Sm on
35919463SmckusickIf
36043084Scael.Ar parameter
36119464Smckusickis set, substitute
36243084Scael.Ar word ;
36319463Smckusickotherwise substitute nothing.
364*50827Scael.El
36543084Scael.Pp
36619463SmckusickIn the above
36743084Scael.Ar word
36819464Smckusickis not evaluated unless it is to be used as the substituted string.
369*50827Scael(So that, for example,
370*50827Scael.Li echo ${d\-'pwd'}
371*50827Scaelwill only execute
37243084Scael.Ar pwd
37319463Smckusickif
37443084Scael.Ar d
37519463Smckusickis unset.)
37643084Scael.Pp
37719463SmckusickThe following
37843084Scael.Ar parameters
37919463Smckusickare automatically set by the shell.
38043084Scael.Pp
381*50827Scael.Bl -tag -width flag -compact
382*50827Scael.It Ic \&#
38319463SmckusickThe number of positional parameters in decimal.
384*50827Scael.It Fl
38519463SmckusickOptions supplied to the shell on invocation or by
38643084Scael.Ar set  .
387*50827Scael.It Ic \&?
38819464SmckusickThe value returned by the last executed command in decimal.
389*50827Scael.It Ic \&$
39019463SmckusickThe process number of this shell.
391*50827Scael.It Ic \&!
39219463SmckusickThe process number of the last background command invoked.
393*50827Scael.El
39443084Scael.Pp
39519463SmckusickThe following
39643084Scael.Ar parameters
39719463Smckusickare used but not set by the shell.
39843084Scael.Pp
399*50827Scael.Bl -tag -width flag -compact
400*50827Scael.It Ev HOME
40119463SmckusickThe default argument (home directory) for the
40243084Scael.Ic cd
40319463Smckusickcommand.
404*50827Scael.It Ev PATH
40519463SmckusickThe search path for commands (see
40643084Scael.Ar execution  ) .
407*50827Scael.It Ev MAIL
40819463SmckusickIf this variable is set to the name of
40919464Smckusicka mail file, the shell informs the user of
41019463Smckusickthe arrival of mail in the specified file.
411*50827Scael.It Ev PS1
41219464SmckusickPrimary prompt string, by default '$ '.
413*50827Scael.It Ev PS2
41419464SmckusickSecondary prompt string, by default '> '.
415*50827Scael.It Ev IFS
41619464SmckusickInternal field separators, normally
41743084Scael.Em space  ,
41843084Scael.Em tab ,
41919463Smckusickand
42043084Scael.Em newline  .
42143084Scael.Ev IFS
42243084Scaelis ignored if
42343084Scael.Nm sh
42427716Smckusickis running as root or if the effective user id differs from the real user id.
425*50827Scael.El
426*50827Scael.Ss Blank interpretation
42719463SmckusickAfter parameter and command substitution,
42819463Smckusickany results of substitution are scanned for internal field separator
42919463Smckusickcharacters (those found in
430*50827Scael.Ic $ Ns Ev IFS )
43119463Smckusickand split into distinct arguments where such characters are found.
43219463SmckusickExplicit null arguments ("" or \'\') are retained.
43319464SmckusickImplicit null arguments (those resulting from
43443084Scael.Em parameters
43519463Smckusickthat have no values) are removed.
436*50827Scael.Ss File name generation
43719464SmckusickFollowing substitution, each command word is scanned for the characters
43843084Scael.Ic \&* ,
43943084Scael.Ic \&?
44019463Smckusickand
44143084Scael.Ic \&[ .
44219464SmckusickIf one of these characters appears, the word is regarded as a pattern.
44319464SmckusickThe word is replaced with alphabetically sorted file names that match the
44419464Smckusickpattern.  If no file name is found that matches the pattern,
44519464Smckusickthe word is left unchanged.  The character
44643084Scael.Ic \&.
44719464Smckusickat the start of a file name or immediately following a
44843084Scael.Ic \&/ ,
44919463Smckusickand the character
45043084Scael.Ic \&/ ,
45119463Smckusickmust be matched explicitly.
452*50827Scael.Pp
453*50827Scael.Bl -tag -width x...x -compact
454*50827Scael.It Ic \&*\&
45519463SmckusickMatches any string, including the null string.
456*50827Scael.It Ic \&?
45719463SmckusickMatches any single character.
458*50827Scael.It Ic \&[...]
45919464SmckusickMatches any one of the characters enclosed.
46019463SmckusickA pair of characters separated by
46143084Scael.Fl
46219464Smckusickmatches any character lexically between the pair.
463*50827Scael.El
464*50827Scael.Ss Quoting
46519463SmckusickThe following characters have a special meaning to the shell
46619463Smckusickand cause termination of a word unless quoted.
467*50827Scael.Bd -filled -offset indent
468*50827Scael.Ic "\&;  \&&  \&(  \&)  \&|  \&<  \&>  newline   space   tab"
469*50827Scael.Ed
47043084Scael.Pp
47119463SmckusickA character may be
47243084Scael.Ar quoted
47319464Smckusickby preceding it with a
474*50827Scael.Sq Ic \e .
475*50827Scael.Ic \enewline
47619463Smckusickis ignored.
47743084ScaelAll characters enclosed between a pair of quote marks
47843084Scael.Pq Sq ,
47943084Scaelexcept a single quote, are quoted.  Inside double quotes
48043084Scael.Pq Dq
48119463Smckusickparameter and command substitution occurs and
482*50827Scael.Ic \e
48319463Smckusickquotes the characters
484*50827Scael.Ic \e' "
48519463Smckusickand
486*50827Scael.Ic \&$ .
48743084Scael.Pp
488*50827Scael.Bd -filled -offset indent -compact
489*50827Scael.Dq Ic \&$\&*
49019463Smckusickis equivalent to
491*50827Scael.Dq Ic "$1 $2" \&...
49219463Smckusickwhereas
493*50827Scael.Ed
494*50827Scael.Bd -filled -offset indent -compact
495*50827Scael.Dq Ic $@
49619463Smckusickis equivalent to
497*50827Scael.Dq Ic "$1"
498*50827Scael.Dq Ic "$2"
499*50827Scael... .
500*50827Scael.Ed
501*50827Scael.Ss Prompting
50219464SmckusickWhen used interactively, the shell prompts with the value of
50343084Scael.Ev PS1
50419463Smckusickbefore reading a command.
50519463SmckusickIf at any time a newline is typed and further input is needed
50619464Smckusickto complete a command, the secondary prompt
507*50827Scael.Ic $ Ns Ev PS2
50819463Smckusickis issued.
509*50827Scael.Ss Input/Output
51019463SmckusickBefore a command is executed its input and output
51119463Smckusickmay be redirected using a special notation interpreted by the shell.
51219463SmckusickThe following may appear anywhere in a simple-command
51319463Smckusickor may precede or follow a
51443084Scael.Ar command
51519463Smckusickand are not passed on to the invoked command.
51619463SmckusickSubstitution occurs before
51743084Scael.Ar word
51819463Smckusickor
51943084Scael.Ar digit
52019463Smckusickis used.
521*50827Scael.Bl -tag -width Ic
522*50827Scael.It Ic \&< Ar word
52319463SmckusickUse file
52443084Scael.Ar word
52519463Smckusickas standard input (file descriptor 0).
526*50827Scael.It Ic \&> Ar word
52719463SmckusickUse file
52843084Scael.Ar word
52919463Smckusickas standard output (file descriptor 1).
53019464SmckusickIf the file does not exist, it is created;
53119463Smckusickotherwise it is truncated to zero length.
532*50827Scael.It Ic \&>\&> Ar word
53319463SmckusickUse file
53443084Scael.Ar word
53519463Smckusickas standard output.
53619464SmckusickIf the file exists, output is appended (by seeking to the end);
53719463Smckusickotherwise the file is created.
538*50827Scael.It Ic \&<\&< Ar word
53919463SmckusickThe shell input is read up to a line the same as
54043084Scael.Ar word  ,
54119463Smckusickor end of file.
54219464SmckusickThe resulting document becomes the standard input.
54319463SmckusickIf any character of
54443084Scael.Ar word
54519464Smckusickis quoted, no interpretation is placed upon the characters of the document;
54619463Smckusickotherwise, parameter and command substitution occurs,
547*50827Scael.Ic \enewline
54819464Smckusickis ignored, and
549*50827Scael.Ic \e
55019463Smckusickis used to quote the characters
551*50827Scael.Ic \&$ \&'
55219463Smckusickand the first character of
55343084Scael.Ar word .
554*50827Scael.It Ic \&<\&& Ar digit
55519463SmckusickThe standard input is duplicated from file descriptor
55643084Scael.Ar digit ;
55719463Smckusicksee
55843084Scael.Xr dup  2  .
55943084ScaelSimilarly for the standard output using
56043084Scael.Ic \&> .
561*50827Scael.It Ic \&< \&\-
56219463SmckusickThe standard input is closed.
56343084ScaelSimilarly for the standard output using
56443084Scael.Ic \&> .
565*50827Scael.El
56643084Scael.Pp
56719464SmckusickIf one of the above is preceded by a digit, the
56819464Smckusickfile descriptor created is that specified by the digit
56919464Smckusick(instead of the default 0 or 1).  For example,
57043084Scael.Pp
57143084Scael.Dl \&... 2>&1
57243084Scael.Pp
57319463Smckusickcreates file descriptor 2 to be a duplicate
57419463Smckusickof file descriptor 1.
57543084Scael.Pp
57619463SmckusickIf a command is followed by
57743084Scael.Ic \&&
57819464Smckusickthen the default standard input for the command is the empty file
57943084Scael.Pq Pa dev/null .
58019463SmckusickOtherwise, the environment for the execution of a command contains the
58119463Smckusickfile descriptors of the invoking shell as modified by input
58219463Smckusickoutput specifications.
58343084Scael.Ss Environment
58419464SmckusickThe environment is a list of name-value pairs that is passed to
58519464Smckusickan executed program in the same way as a normal argument list; see
58643084Scael.Xr execve  2
58719463Smckusickand
58843084Scael.Xr environ  7  .
58919463SmckusickThe shell interacts with the environment in several ways.
59019464SmckusickOn invocation, the shell scans the environment and creates a
59143084Scael.Ar parameter
59219464Smckusickfor each name found, giving it the corresponding value.
59319463SmckusickExecuted commands inherit the same environment.
59419463SmckusickIf the user modifies the values of these
59543084Scael.Ar parameters
59619464Smckusickor creates new ones, none of these affects the environment unless the
59743084Scael.Ic export
59819463Smckusickcommand is used to bind the shell's
59943084Scael.Ar parameter
60019463Smckusickto the environment.
60119463SmckusickThe environment seen by any executed command is thus composed
60219463Smckusickof any unmodified name-value pairs originally inherited by the shell,
60319464Smckusickplus any modifications or additions, all of which must be noted in
60443084Scael.Ic export
60519463Smckusickcommands.
60643084Scael.Pp
60719463SmckusickThe environment for any
60843084Scael.Ar simple-command
60919463Smckusickmay be augmented by prefixing it with one or more assignments to
61043084Scael.Ar parameters .
61119463SmckusickThus these two lines are equivalent
61243084Scael.Pp
61343084Scael.Dl TERM=450 cmd args
61443084Scael.Dl (export TERM; TERM=450; cmd args)
61543084Scael.Pp
61619463SmckusickIf the
61743084Scael.Fl k
61819463Smckusickflag is set,
61943084Scael.Ar all
62019463Smckusickkeyword arguments are placed in the environment,
62143084Scaeleven if they occur after the command name.
622*50827ScaelThe following prints
623*50827Scael.Ql a=b c
624*50827Scaeland
625*50827Scael.Ql c :
626*50827Scael.Bd -literal -offset indent
62719463Smckusickecho a=b c
62819463Smckusickset \-k
62919463Smckusickecho a=b c
630*50827Scael.Ed
631*50827Scael.Ss Signals
632*50827ScaelThe
633*50827Scael.Dv INTERRUPT
634*50827Scaeland
635*50827Scael.Dv QUIT
636*50827Scaelsignals for an invoked
63719463Smckusickcommand are ignored if the command is followed by
63843084Scael.Ic \&& ;
63919464Smckusickotherwise signals have the values inherited by the shell from its parent.
64019463Smckusick(But see also
64143084Scael.Ic trap . )
642*50827Scael.Ss Execution
64319464SmckusickEach time a command is executed the above substitutions are carried out.
64419464SmckusickExcept for the 'special commands' listed below a new process is created and
64519463Smckusickan attempt is made to execute the command via an
64643084Scael.Xr execve  2  .
64743084Scael.Pp
64819463SmckusickThe shell parameter
649*50827Scael.Ic \&$ Ns Ev PATH
65019464Smckusickdefines the search path for the directory containing the command.
65119464SmckusickEach alternative directory name is separated by a colon
652*50827Scael.Pq Ic \&: .
65319463SmckusickThe default path is
65443084Scael.Pa :/bin:/usr/bin .
65543084ScaelIf the command name contains a
656*50827Scael.Ql / ,
65743084Scaelthe search path is not used.
65819464SmckusickOtherwise, each directory in the path is searched for an executable file.
65919463SmckusickIf the file has execute permission but is not an
66043084Scael.Pa a.out
66119464Smckusickfile, it is assumed to be a file containing shell commands.
66219463SmckusickA subshell (i.e., a separate process) is spawned to read it.
66319464SmckusickA parenthesized command is also executed in a subshell.
664*50827Scael.Ss Special commands
66519463SmckusickThe following commands are executed in the shell process
66619463Smckusickand except where specified
66719463Smckusickno input output redirection is permitted for such commands.
668*50827Scael.Bl -tag -width indent
669*50827Scael.It Ic \&#
67027716SmckusickFor non-interactive shells, everything following the
67143084Scael.Ic \&#
67227716Smckusickis treated as a comment, i.e. the rest of the line is ignored.
67327716SmckusickFor interactive shells, the
67443084Scael.Ic \&#
67527716Smckusickhas no special effect.
676*50827Scael.It Ic \&:\&
67719463SmckusickNo effect; the command does nothing.
678*50827Scael.It Ic \&. Ar file
67919463SmckusickRead and execute commands from
68043084Scael.Ar file
68119464Smckusickand return.  The search path
682*50827Scael.Ic \&$ Ns Ev PATH
68319463Smckusickis used to find the directory containing
68443084Scael.Ar file  .
685*50827Scael.It Ic break Op Ar n
68619463SmckusickExit from the enclosing
68743084Scael.Ic for
68819463Smckusickor
68943084Scael.Ic while
69019463Smckusickloop, if any.
69119463SmckusickIf
69243084Scael.Ar n
69319464Smckusickis specified, break
69443084Scael.Ar n
69519463Smckusicklevels.
696*50827Scael.It Ic continue Op Ar n
69719463SmckusickResume the next iteration of the enclosing
69843084Scael.Ic for
69919463Smckusickor
70043084Scael.Ic while
70119464Smckusickloop.  If
70243084Scael.Ar n
70319464Smckusickis specified, resume at the
704*50827Scael.Ar n Ns 'th
70519463Smckusickenclosing loop.
706*50827Scael.It Ic cd Op Ar arg
70719463SmckusickChange the current directory to
70843084Scael.Ar arg .
70919464SmckusickThe shell parameter
710*50827Scael.Ic \&$ Ns Ev HOME
71119463Smckusickis the default
71243084Scael.Ar arg  .
713*50827Scael.It Ic eval Op Ar arg  \&...
71419464SmckusickThe arguments are read as input to the shell
71519463Smckusickand the resulting command(s) executed.
716*50827Scael.It Ic exec Op Ar arg  \&...
71719464SmckusickThe command specified by the arguments is executed in place of this shell
71819463Smckusickwithout creating a new process.
71919463SmckusickInput output arguments may appear and if no other
72019464Smckusickarguments are given cause the shell input output to be modified.
721*50827Scael.It Ic exit Op Ar n
72219464SmckusickCauses a non interactive shell to exit with the exit status specified by
72343084Scael.Ar n .
72419463SmckusickIf
72543084Scael.Ar n
72619464Smckusickis omitted, the exit status is that of the last command executed.
72719463Smckusick(An end of file will also exit from the shell.)
728*50827Scael.It Ic export Op Ar name ...
72919464SmckusickThe given names are marked for automatic export to the
73043084Scael.Ar environment
73119463Smckusickof subsequently-executed commands.
73219464SmckusickIf no arguments are given, a list of exportable names is printed.
733*50827Scael.It Ic login Op Ar arg ...
734*50827ScaelEquivalent to
735*50827Scael.Ql exec login arg ... .
736*50827Scael.It Ic read Ar name \&...
73719463SmckusickOne line is read from the standard input;
73819464Smckusicksuccessive words of the input are assigned to the variables
73943084Scael.Ar name
74019464Smckusickin order, with leftover words to the last variable.
74119463SmckusickThe return code is 0 unless the end-of-file is encountered.
742*50827Scael.It Ic readonly Op Ar name \&...
74319463SmckusickThe given names are marked readonly and
74419463Smckusickthe values of the these names may not be changed
74519463Smckusickby subsequent assignment.
74619464SmckusickIf no arguments are given, a list of all readonly names is printed.
747*50827Scael.It Xo
748*50827Scael.Ic set Oo Fl eknptuvx Op Ar arg ...
749*50827Scael.Oc
750*50827Scael.Xc
75143084ScaelThe set flags are described in the options section at the beginning
75243084Scaelof this man page.
75319463SmckusickThe current set of flags may be found in
75443084Scael.Ic \&$\- .
75543084Scael.Pp
75643084ScaelRemaining arguments after the flag are positional
75719463Smckusickparameters and are assigned, in order, to
75843084Scael.Ic \&$1 ,
75943084Scael.Ic \&$2 ,
76019464Smckusicketc.  If no arguments are given, the values of all names are printed.
761*50827Scael.It Ic shift
76219463SmckusickThe positional parameters from
76343084Scael.Ic \&$2 ...
76419463Smckusickare renamed
76543084Scael.Ic $1 ...
766*50827Scael.It Ic times
76719464SmckusickPrint the accumulated user and system times for processes run from the shell.
768*50827Scael.It Xo
769*50827Scael.Ic trap Op Ar arg
77043084Scael.Op Ar n
771*50827Scael.Ar ...
772*50827Scael.Xc
77343084Scael.Ar Arg
77419464Smckusickis a command to be read and executed when the shell receives signal(s)
77543084Scael.Ar n .
77619463Smckusick(Note that
77743084Scael.Ar arg
77819464Smckusickis scanned once when the trap is set and once when the trap is taken.)
77919464SmckusickTrap commands are executed in order of signal number.  If
78043084Scael.Ar arg
78119464Smckusickis absent, all trap(s)
78243084Scael.Ar n
78319464Smckusickare reset to their original values.
78419463SmckusickIf
78543084Scael.Ar arg
78619463Smckusickis the null
78719464Smckusickstring, this signal is ignored by the shell and by invoked commands.
78819463SmckusickIf
78943084Scael.Ar n
79019464Smckusickis 0, the command
79143084Scael.Ar arg
79219464Smckusickis executed on exit from the shell, otherwise upon receipt of signal
79343084Scael.Ar n
79419463Smckusickas numbered in
79543084Scael.Xr sigvec  2  .
79643084Scael.Ic Trap
79719464Smckusickwith no arguments prints a list of commands associated with each signal number.
798*50827Scael.It Ic umask Op Ar nnn
79919464SmckusickThe user file creation mask is set to the octal value
80043084Scael.Ar nnn
80119463Smckusick(see
80243084Scael.Xr umask  2  ) .
80319463SmckusickIf
80443084Scael.Ar nnn
80519463Smckusickis omitted, the current value of the mask is printed.
806*50827Scael.It Ic wait Op Ar n
80719464SmckusickWait for the specified process and report its termination status.  If
80843084Scael.Ar n
80919464Smckusickis not given, all currently active child processes are waited for.
81019464SmckusickThe return code from this command is that of the process waited for.
811*50827Scael.El
81243084Scael.Sh FILES
813*50827Scael.Bl -tag -width xhome/.profile -compact
814*50827Scael.It Pa $HOME/.profile
815*50827ScaelUser customized environment variables.
816*50827Scael.It Pa /tmp/sh*
817*50827ScaelDefault temporary file directory.
818*50827Scael.It Pa /dev/null
819*50827ScaelBit bucket.
820*50827Scael.El
82143084Scael.Sh SEE ALSO
82243084Scael.Xr csh 1 ,
82343084Scael.Xr test 1 ,
82443084Scael.Xr execve 2 ,
82543084Scael.Xr environ 7
82643084Scael.Sh DIAGNOSTICS
82719464SmckusickErrors detected by the shell, such as syntax errors cause the shell
82819463Smckusickto return a non zero exit status.
82919463SmckusickIf the shell is being used non interactively
83019463Smckusickthen execution of the shell file is abandoned.
83119463SmckusickOtherwise, the shell returns the exit status of
83219463Smckusickthe last command executed (see also
83343084Scael.Ic exit  ) .
83443084Scael.Sh HISTORY
83543084ScaelThe
836*50827Scael.Nm sh
837*50827Scaelshell appeared in
838*50827Scael.At v6 .
839*50827Scael.Sh BUGS
84043084ScaelIf
84143084Scael.Ic \&<\&<
84243084Scaelis used to provide standard input to an asynchronous
84344957Scaelprocess invoked by
84443084Scael.Ic \&& ,
84543084Scaelthe shell gets mixed up about naming the input document.
84643084ScaelA garbage file
84743084Scael.Pa /tmp/sh*
84843084Scaelis created, and the shell complains about
84919463Smckusicknot being able to find the file by another name.
850