Research V7 development
[unix-history] / usr / man / man1 / sh.1
CommitLineData
e0338e45
B
1.ds OK [\|
2.ds CK \|]
3.ds LT \s-2<\s0
4.ds GT \s-2>\s0
5.ds LE \s-2<\s0
6.ds ST *
7.TH SH 1
8.SH NAME
9sh,
10for,
11case,
12if,
13while,
14.BR : ,
15.BR . ,
16break,
17continue,
18cd,
19eval,
20exec,
21exit,
22export,
23login,
24newgrp,
25read,
26readonly,
27set,
28shift,
29times,
30trap,
31umask,
32wait
33\- command language
34.SH SYNOPSIS
35.B sh
36[
37.B \-ceiknrstuvx
38] [ arg ] ...
39.SH DESCRIPTION
40.I Sh
41is a command programming language
42that executes commands read from a terminal
43or a file.
44See
45.B invocation
46for the meaning of arguments to the shell.
47.PP
48.B Commands.
49.br
50A
51.I simple-command
52is a sequence of non blank
53.I words
54separated by blanks (a blank is a
55.B tab
56or a
57.BR space ).
58The first word specifies the name of the command to
59be executed.
60Except as specified below
61the remaining words are passed as arguments
62to the invoked command.
63The command name is passed as argument 0
64(see
65.IR exec (2)).
66The
67.I value
68of a simple-command is its exit status
69if it terminates normally or 200+\fIstatus\fP if
70it terminates abnormally (see
71.IR signal (2)
72for a list of
73status values).
74.LP
75A
76.I pipeline
77is a sequence of one or more
78.I commands
79separated by
80.B \(or.
81The standard output of each command but the last
82is connected by a
83.IR pipe (2)
84to the standard input of the next command.
85Each command is run as a separate process;
86the shell waits for the last command to terminate.
87.LP
88A
89.I list
90is a sequence of one or more
91.I pipelines
92separated by
93.BR ; ,
94.BR & ,
95.B &&
96or
97.B \(or\|\(or
98and optionally terminated by
99.B ;
100or
101.BR & .
102.B ;
103and
104.B &
105have equal precedence
106which is lower than that of
107.B &&
108and
109.BR \(or\|\(or ,
110.B &&
111and
112.B \(or\|\(or
113also have equal precedence.
114A semicolon causes sequential execution; an ampersand causes
115the preceding
116.I pipeline
117to be executed without waiting for it to finish.
118The symbol
119.B &&
120.RB ( \(or\|\(or )
121causes the
122.I list
123following to be executed only if the preceding
124.I pipeline
125returns a zero (non zero) value.
126Newlines may appear in a
127.I list,
128instead of semicolons,
129to delimit commands.
130.LP
131A
132.I command
133is either a simple-command
134or one of the following.
135The value returned by a command is that of the
136last simple-command executed in the command.
137.TP
138\fBfor \fIname\fR \*(OK\fBin \fIword\fR ...\*(CK \fBdo \fIlist \fBdone\fR
139Each time a
140.B for
141command is executed
142.I name
143is set to the next word in the
144.B for
145word list
146If
147.BI in \ word
148\&...
149is omitted then
150.B
151in "$@"
152is assumed.
153Execution ends when there are no more words in the list.
154.TP
155\fBcase \fIword \fBin\fR \*(OK\fIpattern \fR\*(OK \fB\(or \fIpattern \fR\*(CK ... \fB) \fIlist \fB;;\fR\*(CK ... \fBesac\fR
156A
157.B case
158command executes the
159.I list
160associated with the first
161pattern that matches
162.I word.
163The form of the patterns is
164the same as that used for
165file name generation.
166.TP
167\fBif \fIlist \fBthen \fIlist\fR \*(OK\fBelif \fIlist \fBthen \fIlist\fR\*(CK ... \*(OK\fBelse \fIlist\fR\*(CK \fBfi\fR
168The
169.I list
170following
171.B if
172is executed and if it returns zero the
173.I list
174following
175.B then
176is executed.
177Otherwise, the
178.I list
179following
180.B elif
181is executed and if its value is zero
182the
183.I list
184following
185.B then
186is executed.
187Failing that the
188.B else
189.I list
190is executed.
191.TP
192\fBwhile \fIlist\fR \*(OK\fBdo \fIlist\fR\*(CK \fBdone\fR
193A
194.B while
195command repeatedly executes the
196.B while
197.I list
198and if its value is zero executes the
199.B do
200.I list;
201otherwise the loop terminates.
202The value returned by a
203.B while
204command is that
205of the last executed command in the
206.B do
207.I list.
208.B until
209may be used in place of
210.B while
211to negate
212the loop termination test.
213.TP
214.BI ( " list " )
215Execute
216.I list
217in a subshell.
218.TP
219.BI { " list " }
220.I list
221is simply executed.
222.LP
223The following words
224are only recognized as the first word of a command
225and when not quoted.
226.IP
227.B
228if then else elif fi case in esac for while until do done { }
229.PP
230.B Command substitution.
231.br
232The standard output from a command enclosed in
233a pair of grave accents
234.RB ( \`\|\` )
235may be used as part or all
236of a word;
237trailing newlines are removed.
238.PP
239.B Parameter substitution.
240.br
241The character
242.B $
243is used to introduce substitutable
244parameters.
245Positional parameters may be assigned values by
246.BR set .
247Variables may be set by writing
248.IP
249.IB name = value
250[
251.IB name = value
252] ...
253.TP
254$\fB\|{\fIparameter\fB\|}\fR
255A
256.I parameter
257is a sequence of letters, digits or underscores (a
258.IR name ),
259a digit,
260or any of the characters
261.B
262* @ # ? \- $ !\|.
263The value, if any, of the parameter is substituted.
264The braces are required only when
265.I parameter
266is followed by a letter, digit, or underscore
267that is not to be interpreted as part of its name.
268If
269.I parameter
270is a digit then it is a positional parameter.
271If
272.I parameter
273is
274.BR * " or" " @"
275then all the positional
276parameters, starting with
277.SM
278.BR $1 ,
279are substituted
280separated by spaces.
281.SM
282.B $0
283is set from argument zero when the shell
284is invoked.
285.TP
286$\fB\|{\fIparameter\|\-word\|\fB}\fR
287If
288.I parameter
289is set then substitute its value;
290otherwise substitute
291.I word.
292.TP
293$\fB\|{\fIparameter\|\(eq\|word\|\fB}\fR
294If
295.I parameter
296is not set then set it to
297.I word;
298the value of the parameter is then substituted.
299Positional parameters may not be assigned to
300in this way.
301.TP
302$\fB\|{\fIparameter\|?\|word\|\fB}\fR
303If
304.I parameter
305is set then substitute its value;
306otherwise, print
307.I word
308and exit from the shell.
309If
310.I word
311is omitted then a standard message is printed.
312.TP
313$\fB\|{\fIparameter\|\(plword\|\fB}\fR
314If
315.I parameter
316is set then substitute
317.I word;
318otherwise substitute nothing.
319.LP
320In the above
321.I word
322is not evaluated unless it is
323to be used as the substituted string.
324(So that, for example,
325echo ${d\-\`pwd\`}
326will only execute
327.I pwd
328if
329.I d
330is unset.)
331.LP
332The following
333.I parameters
334are automatically set by the shell.
335.RS
336.TP
337.B #
338The number of positional parameters in decimal.
339.PD 0
340.TP
341.B \-
342Options supplied to the shell on invocation or by
343.BR set .
344.TP
345.B ?
346The value returned by the last executed command
347in decimal.
348.TP
349.B $
350The process number of this shell.
351.TP
352.B !
353The process number of the last background command invoked.
354.PD
355.RE
356.LP
357The following
358.I parameters
359are used but not set by the shell.
360.RS
361.TP
362.B
363.SM HOME
364The default argument (home directory) for the
365.B cd
366command.
367.PD 0
368.TP
369.B
370.SM PATH
371The search path for commands (see
372.BR execution ).
373.TP
374.B
375.SM MAIL
376If this variable is set to the name of
377a mail file then the shell informs the user of
378the arrival of mail in the specified file.
379.SM
380.TP
381.B PS1
382Primary prompt string, by default `$ '.
383.TP
384.SM
385.B PS2
386Secondary prompt string, by default `> '.
387.TP
388.SM
389.B IFS
390Internal field separators,
391normally
392.BR space ,
393.BR tab ,
394and
395.BR newline .
396.PD
397.RE
398.PP
399.B Blank interpretation.
400.br
401After parameter and command substitution,
402any results of substitution are scanned for internal field separator
403characters (those found in
404.SM
405.BR $IFS \*S)
406and split into distinct arguments where such characters are found.
407Explicit null arguments ("" or \'\') are retained.
408Implicit null arguments
409(those resulting from
410.I parameters
411that have no values) are removed.
412.PP
413.B File name generation.
414.br
415Following substitution, each command word is scanned for
416the characters
417.BR * ,
418.B ?
419and
420.B \*(OK.
421If one of these characters appears
422then the word is regarded as a pattern.
423The word is replaced with alphabetically sorted file names that match the pattern.
424If no file name is found that matches the pattern then
425the word is left unchanged.
426The character
427.B .
428at the start of a file name
429or immediately following a
430.BR / ,
431and the character
432.BR / ,
433must be matched explicitly.
434.TP
435.B \*(ST
436Matches any string, including the null string.
437.PD 0
438.TP
439.B ?
440Matches any single character.
441.TP
442.B \*(OK...\*(CK
443Matches any one of the characters
444enclosed.
445A pair of characters separated by
446.B \-
447matches any
448character lexically between the pair.
449.PD
450.PP
451.B Quoting.
452.br
453The following characters have a special meaning to the shell
454and cause termination of a word unless quoted.
455.LP
456 \fB; & ( ) \(or \*(LT \*(GT newline space tab\fP
457.LP
458A character may be
459.I quoted
460by preceding
461it with a
462.B
463\\\|.
464.B \\\\newline
465is ignored.
466All characters enclosed between a pair of quote marks (\fB\'\|\'\fP),
467except a single quote,
468are quoted.
469Inside double quotes
470(\fB"\|"\fP)
471parameter and command substitution occurs and
472.B
473\\
474quotes the characters
475.B
476\\ \` "
477and
478.BR $ \|.
479.LP
480.B
481"$*"
482is equivalent to
483.SM
484.B
485"$1 $2 ..."
486whereas
487.br
488.B
489"$@"
490is equivalent to
491.SM
492.B
493"$1" "$2" ... .
494.PP
495.B Prompting.
496.br
497When used interactively,
498the shell prompts with the value of
499.SM
500PS1
501before reading a command.
502If at any time a newline is typed and further input is needed
503to complete a command then the secondary prompt
504.RB ( \s-2$PS2\s0 )
505is issued.
506.PP
507.B Input output.
508.br
509Before a command is executed its input and output
510may be redirected using a special notation interpreted by the shell.
511The following may appear anywhere in a simple-command
512or may precede or follow a
513.I command
514and are not passed on to the invoked command.
515Substitution occurs before
516.I word
517or
518.I digit
519is used.
520.TP
521\*(LT\fI\|word\fP
522Use file
523.I word
524as standard input (file descriptor 0).
525.PD
526.TP
527\*(GT\fI\|word\fP
528Use file
529.I word
530as standard output (file descriptor 1).
531If the file does not exist then it is created;
532otherwise it is truncated to zero length.
533.TP
534\*(GT\*(GT\fI\|word\fP
535Use file
536.I word
537as standard output.
538If the file exists then output is appended (by seeking to the end);
539otherwise the file is created.
540.TP
541\*(LT\*(LT\fI\|word\fP
542The shell input is read up to a line the same as
543.IR word ,
544or end of file.
545The resulting document becomes
546the standard input.
547If any character of
548.I word
549is quoted then no interpretation
550is placed upon the characters of the document;
551otherwise, parameter and command substitution occurs,
552.B
553\\newline
554is ignored,
555and
556.B
557\\
558is used to quote the characters
559.B
560\\ $ \`
561and the first character of
562.I word.
563.TP
564\*(LT\|&\|\fIdigit\fP
565The standard input is duplicated from file descriptor
566.I digit;
567see
568.IR dup (2).
569Similarly for the standard output using \*(GT\|.
570.TP
571\*(LT\|&\|\-
572The standard input is closed.
573Similarly for the standard output using \*(GT\|.
574.PD
575.LP
576If one of the above is preceded by a digit
577then the
578file descriptor created is that specified
579by the digit
580(instead of the default 0 or 1).
581For example,
582.LP
583 \&... 2\*(GT&1
584.LP
585creates file descriptor 2 to be a duplicate
586of file descriptor 1.
587.LP
588If a command is followed by
589.B &
590then the default standard input
591for the command
592is the empty file
593(/dev/null).
594Otherwise, the environment for the execution of a command contains the
595file descriptors of the invoking shell as modified by input
596output specifications.
597.PP
598.B Environment.
599.br
600The environment
601is a list of name-value pairs that is passed to
602an executed program in the same way as a normal argument list;
603see
604.IR exec (2)
605and
606.IR environ (5).
607The shell interacts with the environment in several ways.
608On invocation, the shell scans the environment
609and creates a
610.I parameter
611for each name found,
612giving it the corresponding value.
613Executed commands inherit the same environment.
614If the user modifies the values of these
615.I parameters
616or creates new ones,
617none of these affects the environment
618unless the
619.B export
620command is used to bind the shell's
621.I parameter
622to the environment.
623The environment seen by any executed command is thus composed
624of any unmodified name-value pairs originally inherited by the shell,
625plus any modifications or additions,
626all of which must be noted in
627.B export
628commands.
629.LP
630The environment for any
631.I simple-command
632may be augmented by prefixing it with one or more assignments to
633.I parameters.
634Thus these two lines are equivalent
635.IP
636TERM=450 cmd args
637.br
638(export TERM; TERM=450; cmd args)
639.LP
640If the
641.B \-k
642flag is set,
643.I all
644keyword arguments are placed in the environment,
645even if the occur after the command name.
646The following prints `a=b c' and `c':
647.nf
648echo a=b c
649set \-k
650echo a=b c
651.fi
652.PP
653.B Signals.
654.br
655The INTERRUPT and QUIT signals for an invoked
656command are ignored if the command is followed by
657.BR & ;
658otherwise signals have the values
659inherited by the shell from its parent.
660(But see also
661.BR trap. )
662.PP
663.B Execution.
664.br
665Each time a command is executed the above substitutions
666are carried out.
667Except for the `special commands' listed below a new
668process is created and
669an attempt is made to execute the command via an
670.IR exec (2).
671.LP
672The shell parameter
673.B
674.SM $PATH
675defines the search path for
676the directory containing the command.
677Each alternative directory name is separated by
678a colon
679.RB ( : ).
680The default path is
681.BR :/bin:/usr/bin .
682If the command name contains a / then the search path
683is not used.
684Otherwise, each directory in the path is
685searched for an executable file.
686If the file has execute permission but is not an
687.I a.out
688file,
689it is assumed to be a file containing shell commands.
690A subshell (i.e., a separate process) is spawned to read it.
691A parenthesized command is also executed in
692a subshell.
693.PP
694.B Special commands.
695.br
696The following commands are executed in the shell process
697and except where specified
698no input output redirection is permitted for such commands.
699.TP
700.B :
701No effect; the command does nothing.
702.PD 0
703.TP
704.BI . \ file
705Read and execute commands from
706.I file
707and return.
708The search path
709.B
710.SM $PATH
711is used to find the directory containing
712.IR file .
713.TP
714\fBbreak\fR \*(OK\fIn\fR\*(CK
715Exit from the enclosing
716.B for
717or
718.B while
719loop, if any.
720If
721.I n
722is specified then break
723.I n
724levels.
725.TP
726\fBcontinue\fR \*(OK\fIn\fR\*(CK
727Resume the next iteration of the enclosing
728.B for
729or
730.B while
731loop.
732If
733.I n
734is specified then resume at the
735.IR n -th
736enclosing loop.
737.TP
738\fBcd\fR \*(OK\fIarg\fR\*(CK
739Change the current directory to
740.I arg.
741The shell
742parameter
743.B
744.SM $HOME
745is the default
746.IR arg .
747.TP
748\fBeval\fR \*(OK\fIarg \fR...\*(CK
749The arguments are read as input
750to the shell
751and the resulting command(s) executed.
752.TP
753\fBexec\fR \*(OK\fIarg \fR...\*(CK
754The command specified by
755the arguments is executed in place of this shell
756without creating a new process.
757Input output arguments may appear and if no other
758arguments are given cause the shell input
759output to be modified.
760.TP
761\fBexit\fR \*(OK\fIn\fR\*(CK
762Causes a non interactive shell to exit
763with the exit status specified by
764.I n.
765If
766.I n
767is omitted then the exit status is that of the last command executed.
768(An end of file will also exit from the shell.)
769.TP
770\fBexport\fR \*(OK\fIname\fR ...\*(CK
771The given names are marked
772for automatic export to the
773.I environment
774of subsequently-executed commands.
775If no arguments are given then a list of
776exportable names is printed.
777.TP
778\fBlogin\fR \*(OK\fIarg\fR ...\*(CK
779Equivalent to `exec login arg ...'.
780.TP
781\fBnewgrp\fR \*(OK\fIarg \fR...\*(CK
782Equivalent to `exec newgrp arg ...'.
783.TP
784.BI read \ name\ ...
785One line is read from the standard input;
786successive words of the input are assigned to the
787variables
788.I name
789in order,
790with leftover words to the last variable.
791The return code is 0 unless the end-of-file is encountered.
792.TP
793\fBreadonly\fR \*(OK\fIname \fR...\*(CK
794The given names are marked readonly and
795the values of the these names may not be changed
796by subsequent assignment.
797If no arguments are given then a list
798of all readonly names is printed.
799.TP
800\fBset\fR \*(OK\fB\-eknptuvx\fR \*(OK\fIarg \fR...\*(CK\*(CK
801.RS
802.PD 0
803.TP 3m
804.B \-e
805If non interactive then exit immediately if a command fails.
806.TP
807.B \-k
808All keyword arguments are placed in the environment for a command,
809not just those that precede the command name.
810.TP
811.B \-n
812Read commands but do not execute them.
813.TP
814.B \-t
815Exit after reading and executing one command.
816.TP
817.B \-u
818Treat unset variables as an error when substituting.
819.TP
820.B \-v
821Print shell input lines as they are read.
822.TP
823.B \-x
824Print commands and their arguments as they are executed.
825.TP
826.B \-
827Turn off the
828.B \-x
829and
830.B \-v
831options.
832.PD
833.LP
834These flags can also be used upon invocation of the shell.
835The current set of flags may be found in
836.BR $\- .
837.LP
838Remaining arguments are positional
839parameters and are assigned, in order, to
840.SM
841.BR $1 ,
842.SM
843.BR $2 ,
844etc.
845If no arguments are given then the values
846of all names are printed.
847.RE
848.TP
849.B shift
850The positional parameters from
851.SM
852.BR $2 ...
853are renamed
854.SM
855.BR $1 ...
856.TP
857.B times
858Print the accumulated user and system times for
859processes run from the shell.
860.TP
861\fBtrap\fR \*(OK\fIarg\fR\*(CK \*(OK\fIn\fR\*(CK ...
862.I Arg
863is a command to be read and executed when the shell
864receives signal(s)
865.I n.
866(Note that
867.I arg
868is scanned once when
869the trap is set and once when the trap
870is taken.)
871Trap commands are executed in order of signal number.
872If
873.I arg
874is absent then all trap(s)
875.I n
876are reset
877to their original values.
878If
879.I arg
880is the null
881string then this signal is ignored by the shell and by invoked commands.
882If
883.I n
884is 0 then the command
885.I arg
886is executed
887on exit from the shell,
888otherwise upon receipt of signal
889.I n
890as numbered in
891.IR signal (2).
892.I Trap
893with no arguments prints a list
894of commands associated with each signal number.
895.TP
896\fBumask \fR[ \fInnn\fR ]
897The user file creation mask is set to
898the octal value
899.I nnn
900(see
901.IR umask (2)).
902If
903.I nnn
904is omitted, the current value of the mask is printed.
905.TP
906\fBwait\fP \*(OK\fIn\fP\*(CK
907Wait for the specified process and
908report its termination status.
909If
910.I n
911is not given then all currently active child processes are waited for.
912The return code from this command is that of
913the process waited for.
914.PD
915.LP
916.PP
917.B Invocation.
918.br
919If the first character of argument zero is
920.BR \- ,
921commands are read from
922.BR \s-2$HOME\s0/.\|profile ,
923if such a file exists.
924Commands are then read as described below.
925The following flags are interpreted by the shell
926when it is invoked.
927.PD 0
928.TP 11n
929.BI \-c \ string
930If the
931.B \-c
932flag is present then
933commands are read from
934.I string\|.
935.TP 11n
936.B \-s
937If the
938.B \-s
939flag is present or if no
940arguments remain
941then commands are read from the standard input.
942Shell output is written to
943file descriptor 2.
944.TP 11n
945.B \-i
946If the
947.B \-i
948flag is present or
949if the shell input and output are attached to a terminal (as told by
950.IR gtty )
951then this shell is
952.I interactive.
953In this case the terminate signal
954SIGTERM (see
955.IR signal (2))
956is ignored (so that `kill 0'
957does not kill an interactive shell) and the interrupt signal
958SIGINT is caught and ignored
959(so that
960.B wait
961is interruptable).
962In all cases SIGQUIT is ignored by the shell.
963.PD
964.LP
965The remaining flags and arguments are described under the
966.B set
967command.
968.SH FILES
969.RB $HOME/ . \^profile
970.br
971/tmp/sh*
972.br
973/dev/null
974.SH SEE ALSO
975test(1),
976exec(2),
977.SH DIAGNOSTICS
978Errors detected by the shell, such as syntax errors
979cause the shell
980to return a non zero exit status.
981If the shell is being used non interactively
982then execution of the shell file is abandoned.
983Otherwise, the shell returns the exit status of
984the last command executed (see also
985.BR exit ).
986.SH BUGS
987If \*(LT\*(LT is used to provide standard input to an asynchronous
988process invoked by &,
989the shell gets mixed up about naming the input document.
990A garbage file /tmp/sh* is created, and the shell complains about
991not being able to find the file by another name.