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