xref: /freebsd/bin/sh/sh.1 (revision 70ed590b393173d4ea697be2a27054ed171f0c1a)
1.\"-
2.\" Copyright (c) 1991, 1993
3.\"	The Regents of the University of California.  All rights reserved.
4.\"
5.\" This code is derived from software contributed to Berkeley by
6.\" Kenneth Almquist.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	from: @(#)sh.1	8.6 (Berkeley) 5/4/95
33.\" $FreeBSD$
34.\"
35.Dd April 5, 2010
36.Dt SH 1
37.Os
38.Sh NAME
39.Nm sh
40.Nd command interpreter (shell)
41.Sh SYNOPSIS
42.Nm
43.Op Fl /+abCEefIimnPpsTuVvx
44.Op Fl /+o Ar longname
45.Op Fl c Ar string
46.Op Ar arg ...
47.Sh DESCRIPTION
48The
49.Nm
50utility is the standard command interpreter for the system.
51The current version of
52.Nm
53is in the process of being changed to
54conform with the
55.St -p1003.2
56specification for the shell.
57This version has many features which make
58it appear
59similar in some respects to the Korn shell, but it is not a Korn
60shell clone like
61.Nm pdksh .
62Only features
63designated by
64.Tn POSIX ,
65plus a few Berkeley extensions, are being
66incorporated into this shell.
67This man page is not intended to be a tutorial nor a complete
68specification of the shell.
69.Ss Overview
70The shell is a command that reads lines from
71either a file or the terminal, interprets them, and
72generally executes other commands.
73It is the program that is started when a user logs into the system,
74although a user can select a different shell with the
75.Xr chsh 1
76command.
77The shell
78implements a language that has flow control constructs,
79a macro facility that provides a variety of features in
80addition to data storage, along with built-in history and line
81editing capabilities.
82It incorporates many features to
83aid interactive use and has the advantage that the interpretative
84language is common to both interactive and non-interactive
85use (shell scripts).
86That is, commands can be typed directly
87to the running shell or can be put into a file,
88which can be executed directly by the shell.
89.Ss Invocation
90.\"
91.\" XXX This next sentence is incredibly confusing.
92.\"
93If no arguments are present and if the standard input of the shell
94is connected to a terminal
95(or if the
96.Fl i
97option is set),
98the shell is considered an interactive shell.
99An interactive shell
100generally prompts before each command and handles programming
101and command errors differently (as described below).
102When first starting, the shell inspects argument 0, and
103if it begins with a dash
104.Pq Ql - ,
105the shell is also considered a login shell.
106This is normally done automatically by the system
107when the user first logs in.
108A login shell first reads commands
109from the files
110.Pa /etc/profile
111and then
112.Pa .profile
113in a user's home directory,
114if they exist.
115If the environment variable
116.Ev ENV
117is set on entry to a shell, or is set in the
118.Pa .profile
119of a login shell, the shell then reads commands from the file named in
120.Ev ENV .
121Therefore, a user should place commands that are to be executed only
122at login time in the
123.Pa .profile
124file, and commands that are executed for every shell inside the
125.Ev ENV
126file.
127The user can set the
128.Ev ENV
129variable to some file by placing the following line in the file
130.Pa .profile
131in the home directory,
132substituting for
133.Pa .shinit
134the filename desired:
135.Pp
136.Dl "ENV=$HOME/.shinit; export ENV"
137.Pp
138The first non-option argument specified on the command line
139will be treated as the
140name of a file from which to read commands (a shell script), and
141the remaining arguments are set as the positional parameters
142of the shell
143.Li ( $1 , $2 ,
144etc.).
145Otherwise, the shell reads commands
146from its standard input.
147.Pp
148Unlike older versions of
149.Nm
150the
151.Ev ENV
152script is only sourced on invocation of interactive shells.
153This
154closes a well-known, and sometimes easily exploitable security
155hole related to poorly thought out
156.Ev ENV
157scripts.
158.Ss Argument List Processing
159All of the single letter options to
160.Nm
161have a corresponding long name,
162with the exception of
163.Fl c
164and
165.Fl /+o .
166These long names are provided next to the single letter options
167in the descriptions below.
168The long name for an option may be specified as an argument to the
169.Fl /+o
170option of
171.Nm .
172Once the shell is running,
173the long name for an option may be specified as an argument to the
174.Fl /+o
175option of the
176.Ic set
177built-in command
178(described later in the section called
179.Sx Built-in Commands ) .
180Introducing an option with a dash
181.Pq Ql -
182enables the option,
183while using a plus
184.Pq Ql +
185disables the option.
186A
187.Dq Li --
188or plain
189.Ql -
190will stop option processing and will force the remaining
191words on the command line to be treated as arguments.
192The
193.Fl /+o
194and
195.Fl c
196options do not have long names.
197They take arguments and are described after the single letter options.
198.Bl -tag -width indent
199.It Fl a Li allexport
200Flag variables for export when assignments are made to them.
201.It Fl b Li notify
202Enable asynchronous notification of background job
203completion.
204(UNIMPLEMENTED)
205.It Fl C Li noclobber
206Do not overwrite existing files with
207.Ql > .
208.It Fl E Li emacs
209Enable the built-in
210.Xr emacs 1
211command line editor (disables the
212.Fl V
213option if it has been set;
214set automatically when interactive on terminals).
215.It Fl e Li errexit
216Exit immediately if any untested command fails in non-interactive mode.
217The exit status of a command is considered to be
218explicitly tested if the command is part of the list used to control
219an
220.Ic if , elif , while ,
221or
222.Ic until ;
223if the command is the left
224hand operand of an
225.Dq Li &&
226or
227.Dq Li ||
228operator; or if the command is a pipeline preceded by the
229.Ic !\&
230operator.
231If a shell function is executed and its exit status is explicitly
232tested, all commands of the function are considered to be tested as
233well.
234.It Fl f Li noglob
235Disable pathname expansion.
236.It Fl I Li ignoreeof
237Ignore
238.Dv EOF Ap s
239from input when in interactive mode.
240.It Fl i Li interactive
241Force the shell to behave interactively.
242.It Fl m Li monitor
243Turn on job control (set automatically when interactive).
244.It Fl n Li noexec
245If not interactive, read commands but do not
246execute them.
247This is useful for checking the
248syntax of shell scripts.
249.It Fl P Li physical
250Change the default for the
251.Ic cd
252and
253.Ic pwd
254commands from
255.Fl L
256(logical directory layout)
257to
258.Fl P
259(physical directory layout).
260.It Fl p Li privileged
261Turn on privileged mode.
262This mode is enabled on startup
263if either the effective user or group ID is not equal to the
264real user or group ID.
265Turning this mode off sets the
266effective user and group IDs to the real user and group IDs.
267When this mode is enabled for interactive shells, the file
268.Pa /etc/suid_profile
269is sourced instead of
270.Pa ~/.profile
271after
272.Pa /etc/profile
273is sourced, and the contents of the
274.Ev ENV
275variable are ignored.
276.It Fl s Li stdin
277Read commands from standard input (set automatically
278if no file arguments are present).
279This option has
280no effect when set after the shell has already started
281running (i.e., when set with the
282.Ic set
283command).
284.It Fl T Li trapsasync
285When waiting for a child, execute traps immediately.
286If this option is not set,
287traps are executed after the child exits,
288as specified in
289.St -p1003.2 .
290This nonstandard option is useful for putting guarding shells around
291children that block signals.
292The surrounding shell may kill the child
293or it may just return control to the tty and leave the child alone,
294like this:
295.Bd -literal -offset indent
296sh -T -c "trap 'exit 1' 2 ; some-blocking-program"
297.Ed
298.It Fl u Li nounset
299Write a message to standard error when attempting
300to expand a variable, a positional parameter or
301the special parameter
302.Va \&!
303that is not set, and if the
304shell is not interactive, exit immediately.
305.It Fl V Li vi
306Enable the built-in
307.Xr vi 1
308command line editor (disables
309.Fl E
310if it has been set).
311.It Fl v Li verbose
312The shell writes its input to standard error
313as it is read.
314Useful for debugging.
315.It Fl x Li xtrace
316Write each command
317(preceded by the value of the
318.Va PS4
319variable)
320to standard error before it is executed.
321Useful for debugging.
322.El
323.Pp
324The
325.Fl c
326option causes the commands to be read from the
327.Ar string
328operand instead of from the standard input.
329Keep in mind that this option only accepts a single string as its
330argument, hence multi-word strings must be quoted.
331.Pp
332The
333.Fl /+o
334option takes as its only argument the long name of an option
335to be enabled or disabled.
336For example, the following two invocations of
337.Nm
338both enable the built-in
339.Xr emacs 1
340command line editor:
341.Bd -literal -offset indent
342set -E
343set -o emacs
344.Ed
345.Pp
346If used without an argument, the
347.Fl o
348option displays the current option settings in a human-readable format.
349If
350.Cm +o
351is used without an argument, the current option settings are output
352in a format suitable for re-input into the shell.
353.Ss Lexical Structure
354The shell reads input in terms of lines from a file and breaks
355it up into words at whitespace (blanks and tabs), and at
356certain sequences of
357characters called
358.Dq operators ,
359which are special to the shell.
360There are two types of operators: control operators and
361redirection operators (their meaning is discussed later).
362The following is a list of valid operators:
363.Bl -tag -width indent
364.It Control operators:
365.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
366.It Li & Ta Li && Ta Li ( Ta Li ) Ta Li \en
367.It Li ;; Ta Li ; Ta Li | Ta Li ||
368.El
369.It Redirection operators:
370.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
371.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <>
372.It Li <& Ta Li >& Ta Li <<- Ta Li >|
373.El
374.El
375.Pp
376The character
377.Ql #
378introduces a comment if used at the beginning of a word.
379The word starting with
380.Ql #
381and the rest of the line are ignored.
382.Pp
383.Tn ASCII
384.Dv NUL
385characters (character code 0) are not allowed in shell input.
386.Ss Quoting
387Quoting is used to remove the special meaning of certain characters
388or words to the shell, such as operators, whitespace, keywords,
389or alias names.
390.Pp
391There are three types of quoting: matched single quotes,
392matched double quotes, and backslash.
393.Bl -tag -width indent
394.It Single Quotes
395Enclosing characters in single quotes preserves the literal
396meaning of all the characters (except single quotes, making
397it impossible to put single-quotes in a single-quoted string).
398.It Double Quotes
399Enclosing characters within double quotes preserves the literal
400meaning of all characters except dollar sign
401.Pq Ql $ ,
402backquote
403.Pq Ql ` ,
404and backslash
405.Pq Ql \e .
406The backslash inside double quotes is historically weird.
407It remains literal unless it precedes the following characters,
408which it serves to quote:
409.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
410.It Li $ Ta Li ` Ta Li \&" Ta Li \e\  Ta Li \en
411.El
412.It Backslash
413A backslash preserves the literal meaning of the following
414character, with the exception of the newline character
415.Pq Ql \en .
416A backslash preceding a newline is treated as a line continuation.
417.El
418.Ss Reserved Words
419Reserved words are words that have special meaning to the
420shell and are recognized at the beginning of a line and
421after a control operator.
422The following are reserved words:
423.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
424.It Li \&! Ta { Ta } Ta Ic case Ta Ic do
425.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
426.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
427.El
428.Ss Aliases
429An alias is a name and corresponding value set using the
430.Ic alias
431built-in command.
432Whenever a reserved word may occur (see above),
433and after checking for reserved words, the shell
434checks the word to see if it matches an alias.
435If it does, it replaces it in the input stream with its value.
436For example, if there is an alias called
437.Dq Li lf
438with the value
439.Dq Li "ls -F" ,
440then the input
441.Pp
442.Dl "lf foobar"
443.Pp
444would become
445.Pp
446.Dl "ls -F foobar"
447.Pp
448Aliases provide a convenient way for naive users to
449create shorthands for commands without having to learn how
450to create functions with arguments.
451They can also be
452used to create lexically obscure code.
453This use is discouraged.
454.Pp
455An alias name may be escaped in a command line, so that it is not
456replaced by its alias value, by using quoting characters within or
457adjacent to the alias name.
458This is most often done by prefixing
459an alias name with a backslash to execute a function, built-in, or
460normal program with the same name.
461See the
462.Sx Quoting
463subsection.
464.Ss Commands
465The shell interprets the words it reads according to a
466language, the specification of which is outside the scope
467of this man page (refer to the BNF in the
468.St -p1003.2
469document).
470Essentially though, a line is read and if
471the first word of the line (or after a control operator)
472is not a reserved word, then the shell has recognized a
473simple command.
474Otherwise, a complex command or some
475other special construct may have been recognized.
476.Ss Simple Commands
477If a simple command has been recognized, the shell performs
478the following actions:
479.Bl -enum
480.It
481Leading words of the form
482.Dq Li name=value
483are stripped off and assigned to the environment of
484the simple command.
485Redirection operators and
486their arguments (as described below) are stripped
487off and saved for processing.
488.It
489The remaining words are expanded as described in
490the section called
491.Sx Word Expansions ,
492and the first remaining word is considered the command
493name and the command is located.
494The remaining
495words are considered the arguments of the command.
496If no command name resulted, then the
497.Dq Li name=value
498variable assignments recognized in 1) affect the
499current shell.
500.It
501Redirections are performed as described in
502the next section.
503.El
504.Ss Redirections
505Redirections are used to change where a command reads its input
506or sends its output.
507In general, redirections open, close, or
508duplicate an existing reference to a file.
509The overall format
510used for redirection is:
511.Pp
512.D1 Oo Ar n Oc Ar redir-op file
513.Pp
514The
515.Ar redir-op
516is one of the redirection operators mentioned
517previously.
518The following gives some examples of how these
519operators can be used.
520Note that stdin and stdout are commonly used abbreviations
521for standard input and standard output respectively.
522.Bl -tag -width "1234567890XX" -offset indent
523.It Oo Ar n Oc Ns Li > Ar file
524redirect stdout (or file descriptor
525.Ar n )
526to
527.Ar file
528.It Oo Ar n Oc Ns Li >| Ar file
529same as above, but override the
530.Fl C
531option
532.It Oo Ar n Oc Ns Li >> Ar file
533append stdout (or file descriptor
534.Ar n )
535to
536.Ar file
537.It Oo Ar n Oc Ns Li < Ar file
538redirect stdin (or file descriptor
539.Ar n )
540from
541.Ar file
542.It Oo Ar n Oc Ns Li <> Ar file
543redirect stdin (or file descriptor
544.Ar n )
545to and from
546.Ar file
547.It Oo Ar n1 Oc Ns Li <& Ns Ar n2
548duplicate stdin (or file descriptor
549.Ar n1 )
550from file descriptor
551.Ar n2
552.It Oo Ar n Oc Ns Li <&-
553close stdin (or file descriptor
554.Ar n )
555.It Oo Ar n1 Oc Ns Li >& Ns Ar n2
556duplicate stdout (or file descriptor
557.Ar n1 )
558to file descriptor
559.Ar n2
560.It Oo Ar n Oc Ns Li >&-
561close stdout (or file descriptor
562.Ar n )
563.El
564.Pp
565The following redirection is often called a
566.Dq here-document .
567.Bd -unfilled -offset indent
568.Oo Ar n Oc Ns Li << Ar delimiter
569.D1 Ar here-doc-text
570.D1 ...
571.Ar delimiter
572.Ed
573.Pp
574All the text on successive lines up to the delimiter is
575saved away and made available to the command on standard
576input, or file descriptor
577.Ar n
578if it is specified.
579If the
580.Ar delimiter
581as specified on the initial line is quoted, then the
582.Ar here-doc-text
583is treated literally, otherwise the text is subjected to
584parameter expansion, command substitution, and arithmetic
585expansion (as described in the section on
586.Sx Word Expansions ) .
587If the operator is
588.Dq Li <<-
589instead of
590.Dq Li << ,
591then leading tabs
592in the
593.Ar here-doc-text
594are stripped.
595.Ss Search and Execution
596There are three types of commands: shell functions,
597built-in commands, and normal programs.
598The command is searched for (by name) in that order.
599The three types of commands are all executed in a different way.
600.Pp
601When a shell function is executed, all of the shell positional
602parameters (except
603.Li $0 ,
604which remains unchanged) are
605set to the arguments of the shell function.
606The variables which are explicitly placed in the environment of
607the command (by placing assignments to them before the
608function name) are made local to the function and are set
609to the values given.
610Then the command given in the function definition is executed.
611The positional parameters are restored to their original values
612when the command completes.
613This all occurs within the current shell.
614.Pp
615Shell built-in commands are executed internally to the shell, without
616spawning a new process.
617There are two kinds of built-in commands: regular and special.
618Assignments before special builtins persist after they finish
619executing and assignment errors, redirection errors and certain
620operand errors cause a script to be aborted.
621Both regular and special builtins can affect the shell in ways
622normal programs cannot.
623.Pp
624Otherwise, if the command name does not match a function
625or built-in command, the command is searched for as a normal
626program in the file system (as described in the next section).
627When a normal program is executed, the shell runs the program,
628passing the arguments and the environment to the program.
629If the program is not a normal executable file
630(i.e., if it does not begin with the
631.Dq "magic number"
632whose
633.Tn ASCII
634representation is
635.Dq Li #! ,
636resulting in an
637.Er ENOEXEC
638return value from
639.Xr execve 2 )
640the shell will interpret the program in a subshell.
641The child shell will reinitialize itself in this case,
642so that the effect will be
643as if a new shell had been invoked to handle the ad-hoc shell script,
644except that the location of hashed commands located in
645the parent shell will be remembered by the child
646(see the description of the
647.Ic hash
648built-in command below).
649.Pp
650Note that previous versions of this document
651and the source code itself misleadingly and sporadically
652refer to a shell script without a magic number
653as a
654.Dq "shell procedure" .
655.Ss Path Search
656When locating a command, the shell first looks to see if
657it has a shell function by that name.
658Then it looks for a
659built-in command by that name.
660If a built-in command is not found,
661one of two things happen:
662.Bl -enum
663.It
664Command names containing a slash are simply executed without
665performing any searches.
666.It
667The shell searches each entry in the
668.Ev PATH
669environment variable
670in turn for the command.
671The value of the
672.Ev PATH
673variable should be a series of
674entries separated by colons.
675Each entry consists of a
676directory name.
677The current directory
678may be indicated implicitly by an empty directory name,
679or explicitly by a single period.
680.El
681.Ss Command Exit Status
682Each command has an exit status that can influence the behavior
683of other shell commands.
684The paradigm is that a command exits
685with zero for normal or success, and non-zero for failure,
686error, or a false indication.
687The man page for each command
688should indicate the various exit codes and what they mean.
689Additionally, the built-in commands return exit codes, as does
690an executed shell function.
691.Pp
692If a command is terminated by a signal, its exit status is 128 plus
693the signal number.
694Signal numbers are defined in the header file
695.In sys/signal.h .
696.Ss Complex Commands
697Complex commands are combinations of simple commands
698with control operators or reserved words, together creating a larger complex
699command.
700More generally, a command is one of the following:
701.Bl -item -offset indent
702.It
703simple command
704.It
705pipeline
706.It
707list or compound-list
708.It
709compound command
710.It
711function definition
712.El
713.Pp
714Unless otherwise stated, the exit status of a command is
715that of the last simple command executed by the command.
716.Ss Pipelines
717A pipeline is a sequence of one or more commands separated
718by the control operator
719.Ql \&| .
720The standard output of all but
721the last command is connected to the standard input
722of the next command.
723The standard output of the last
724command is inherited from the shell, as usual.
725.Pp
726The format for a pipeline is:
727.Pp
728.D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ...
729.Pp
730The standard output of
731.Ar command1
732is connected to the standard input of
733.Ar command2 .
734The standard input, standard output, or
735both of a command is considered to be assigned by the
736pipeline before any redirection specified by redirection
737operators that are part of the command.
738.Pp
739If the pipeline is not in the background (discussed later),
740the shell waits for all commands to complete.
741.Pp
742If the reserved word
743.Ic !\&
744does not precede the pipeline, the
745exit status is the exit status of the last command specified
746in the pipeline.
747Otherwise, the exit status is the logical
748NOT of the exit status of the last command.
749That is, if
750the last command returns zero, the exit status is 1; if
751the last command returns greater than zero, the exit status
752is zero.
753.Pp
754Because pipeline assignment of standard input or standard
755output or both takes place before redirection, it can be
756modified by redirection.
757For example:
758.Pp
759.Dl "command1 2>&1 | command2"
760.Pp
761sends both the standard output and standard error of
762.Ar command1
763to the standard input of
764.Ar command2 .
765.Pp
766A
767.Ql \&;
768or newline terminator causes the preceding
769AND-OR-list
770(described below in the section called
771.Sx Short-Circuit List Operators )
772to be executed sequentially;
773an
774.Ql &
775causes asynchronous execution of the preceding AND-OR-list.
776.Pp
777Note that unlike some other shells,
778.Nm
779executes each process in the pipeline as a child of the
780.Nm
781process.
782Shell built-in commands are the exception to this rule.
783They are executed in the current shell, although they do not affect its
784environment when used in pipelines.
785.Ss Background Commands (&)
786If a command is terminated by the control operator ampersand
787.Pq Ql & ,
788the shell executes the command asynchronously;
789the shell does not wait for the command to finish
790before executing the next command.
791.Pp
792The format for running a command in background is:
793.Pp
794.D1 Ar command1 Li & Op Ar command2 Li & Ar ...
795.Pp
796If the shell is not interactive, the standard input of an
797asynchronous command is set to
798.Pa /dev/null .
799.Ss Lists (Generally Speaking)
800A list is a sequence of zero or more commands separated by
801newlines, semicolons, or ampersands,
802and optionally terminated by one of these three characters.
803The commands in a
804list are executed in the order they are written.
805If command is followed by an ampersand, the shell starts the
806command and immediately proceeds onto the next command;
807otherwise it waits for the command to terminate before
808proceeding to the next one.
809.Ss Short-Circuit List Operators
810.Dq Li &&
811and
812.Dq Li ||
813are AND-OR list operators.
814.Dq Li &&
815executes the first command, and then executes the second command
816if the exit status of the first command is zero.
817.Dq Li ||
818is similar, but executes the second command if the exit
819status of the first command is nonzero.
820.Dq Li &&
821and
822.Dq Li ||
823both have the same priority.
824.Ss Flow-Control Constructs (if, while, for, case)
825The syntax of the
826.Ic if
827command is:
828.Bd -unfilled -offset indent -compact
829.Ic if Ar list
830.Ic then Ar list
831.Oo Ic elif Ar list
832.Ic then Ar list Oc Ar ...
833.Op Ic else Ar list
834.Ic fi
835.Ed
836.Pp
837The syntax of the
838.Ic while
839command is:
840.Bd -unfilled -offset indent -compact
841.Ic while Ar list
842.Ic do Ar list
843.Ic done
844.Ed
845.Pp
846The two lists are executed repeatedly while the exit status of the
847first list is zero.
848The
849.Ic until
850command is similar, but has the word
851.Ic until
852in place of
853.Ic while ,
854which causes it to
855repeat until the exit status of the first list is zero.
856.Pp
857The syntax of the
858.Ic for
859command is:
860.Bd -unfilled -offset indent -compact
861.Ic for Ar variable Op Ic in Ar word ...
862.Ic do Ar list
863.Ic done
864.Ed
865.Pp
866If
867.Ic in
868and the following words are omitted,
869.Ic in Li \&"$@\&"
870is used instead.
871The words are expanded, and then the list is executed
872repeatedly with the variable set to each word in turn.
873The
874.Ic do
875and
876.Ic done
877commands may be replaced with
878.Ql {
879and
880.Ql } .
881.Pp
882The syntax of the
883.Ic break
884and
885.Ic continue
886commands is:
887.D1 Ic break Op Ar num
888.D1 Ic continue Op Ar num
889.Pp
890The
891.Ic break
892command terminates the
893.Ar num
894innermost
895.Ic for
896or
897.Ic while
898loops.
899The
900.Ic continue
901command continues with the next iteration of the innermost loop.
902These are implemented as special built-in commands.
903.Pp
904The syntax of the
905.Ic case
906command is:
907.Bd -unfilled -offset indent -compact
908.Ic case Ar word Ic in
909.Ar pattern Ns Li ) Ar list Li ;;
910.Ar ...
911.Ic esac
912.Ed
913.Pp
914The pattern can actually be one or more patterns
915(see
916.Sx Shell Patterns
917described later),
918separated by
919.Ql \&|
920characters.
921The exit code of the
922.Ic case
923command is the exit code of the last command executed in the list or
924zero if no patterns were matched.
925.Ss Grouping Commands Together
926Commands may be grouped by writing either
927.Pp
928.D1 Li \&( Ns Ar list Ns Li \%)
929.Pp
930or
931.Pp
932.D1 Li { Ar list Ns Li \&; }
933.Pp
934The first form executes the commands in a subshell.
935Note that built-in commands thus executed do not affect the current shell.
936The second form does not fork another shell,
937so it is slightly more efficient.
938Grouping commands together this way allows the user to
939redirect their output as though they were one program:
940.Bd -literal -offset indent
941{ echo -n "hello"; echo " world"; } > greeting
942.Ed
943.Ss Functions
944The syntax of a function definition is
945.Pp
946.D1 Ar name Li \&( \&) Ar command
947.Pp
948A function definition is an executable statement; when
949executed it installs a function named
950.Ar name
951and returns an
952exit status of zero.
953The
954.Ar command
955is normally a list
956enclosed between
957.Ql {
958and
959.Ql } .
960.Pp
961Variables may be declared to be local to a function by
962using the
963.Ic local
964command.
965This should appear as the first statement of a function,
966and the syntax is:
967.Pp
968.D1 Ic local Oo Ar variable ... Oc Op Fl
969.Pp
970The
971.Ic local
972command is implemented as a built-in command.
973.Pp
974When a variable is made local, it inherits the initial
975value and exported and readonly flags from the variable
976with the same name in the surrounding scope, if there is
977one.
978Otherwise, the variable is initially unset.
979The shell
980uses dynamic scoping, so that if the variable
981.Va x
982is made local to function
983.Em f ,
984which then calls function
985.Em g ,
986references to the variable
987.Va x
988made inside
989.Em g
990will refer to the variable
991.Va x
992declared inside
993.Em f ,
994not to the global variable named
995.Va x .
996.Pp
997The only special parameter that can be made local is
998.Ql - .
999Making
1000.Ql -
1001local causes any shell options that are
1002changed via the
1003.Ic set
1004command inside the function to be
1005restored to their original values when the function
1006returns.
1007.Pp
1008The syntax of the
1009.Ic return
1010command is
1011.Pp
1012.D1 Ic return Op Ar exitstatus
1013.Pp
1014It terminates the current executional scope, returning from the previous
1015nested function, sourced script, or shell instance, in that order.
1016The
1017.Ic return
1018command is implemented as a special built-in command.
1019.Ss Variables and Parameters
1020The shell maintains a set of parameters.
1021A parameter
1022denoted by a name is called a variable.
1023When starting up,
1024the shell turns all the environment variables into shell
1025variables.
1026New variables can be set using the form
1027.Pp
1028.D1 Ar name Ns = Ns Ar value
1029.Pp
1030Variables set by the user must have a name consisting solely
1031of alphabetics, numerics, and underscores.
1032The first letter of a variable name must not be numeric.
1033A parameter can also be denoted by a number
1034or a special character as explained below.
1035.Ss Positional Parameters
1036A positional parameter is a parameter denoted by a number greater than zero.
1037The shell sets these initially to the values of its command line
1038arguments that follow the name of the shell script.
1039The
1040.Ic set
1041built-in command can also be used to set or reset them.
1042.Ss Special Parameters
1043A special parameter is a parameter denoted by a special one-character
1044name.
1045The special parameters recognized by the
1046.Nm
1047shell of
1048.Fx
1049are shown in the following list, exactly as they would appear in input
1050typed by the user or in the source of a shell script.
1051.Bl -hang
1052.It Li $*
1053Expands to the positional parameters, starting from one.
1054When
1055the expansion occurs within a double-quoted string
1056it expands to a single field with the value of each parameter
1057separated by the first character of the
1058.Va IFS
1059variable,
1060or by a space if
1061.Va IFS
1062is unset.
1063.It Li $@
1064Expands to the positional parameters, starting from one.
1065When
1066the expansion occurs within double-quotes, each positional
1067parameter expands as a separate argument.
1068If there are no positional parameters, the
1069expansion of
1070.Li @
1071generates zero arguments, even when
1072.Li @
1073is double-quoted.
1074What this basically means, for example, is
1075if
1076.Li $1
1077is
1078.Dq Li abc
1079and
1080.Li $2
1081is
1082.Dq Li "def ghi" ,
1083then
1084.Li \&"$@\&"
1085expands to
1086the two arguments:
1087.Bd -literal -offset indent
1088"abc"   "def ghi"
1089.Ed
1090.It Li $#
1091Expands to the number of positional parameters.
1092.It Li $?
1093Expands to the exit status of the most recent pipeline.
1094.It Li $-
1095(hyphen) Expands to the current option flags (the single-letter
1096option names concatenated into a string) as specified on
1097invocation, by the
1098.Ic set
1099built-in command, or implicitly
1100by the shell.
1101.It Li $$
1102Expands to the process ID of the invoked shell.
1103A subshell
1104retains the same value of
1105.Va $
1106as its parent.
1107.It Li $!
1108Expands to the process ID of the most recent background
1109command executed from the current shell.
1110For a
1111pipeline, the process ID is that of the last command in the
1112pipeline.
1113.It Li $0
1114(zero) Expands to the name of the shell or shell script.
1115.El
1116.Ss Word Expansions
1117This clause describes the various expansions that are
1118performed on words.
1119Not all expansions are performed on
1120every word, as explained later.
1121.Pp
1122Tilde expansions, parameter expansions, command substitutions,
1123arithmetic expansions, and quote removals that occur within
1124a single word expand to a single field.
1125It is only field
1126splitting or pathname expansion that can create multiple
1127fields from a single word.
1128The single exception to this rule is
1129the expansion of the special parameter
1130.Va @
1131within double-quotes,
1132as was described above.
1133.Pp
1134The order of word expansion is:
1135.Bl -enum
1136.It
1137Tilde Expansion, Parameter Expansion, Command Substitution,
1138Arithmetic Expansion (these all occur at the same time).
1139.It
1140Field Splitting is performed on fields generated by step (1)
1141unless the
1142.Va IFS
1143variable is null.
1144.It
1145Pathname Expansion (unless the
1146.Fl f
1147option is in effect).
1148.It
1149Quote Removal.
1150.El
1151.Pp
1152The
1153.Ql $
1154character is used to introduce parameter expansion, command
1155substitution, or arithmetic evaluation.
1156.Ss Tilde Expansion (substituting a user's home directory)
1157A word beginning with an unquoted tilde character
1158.Pq Ql ~
1159is
1160subjected to tilde expansion.
1161All the characters up to a slash
1162.Pq Ql /
1163or the end of the word are treated as a username
1164and are replaced with the user's home directory.
1165If the
1166username is missing (as in
1167.Pa ~/foobar ) ,
1168the tilde is replaced with the value of the
1169.Ev HOME
1170variable (the current user's home directory).
1171.Ss Parameter Expansion
1172The format for parameter expansion is as follows:
1173.Pp
1174.D1 Li ${ Ns Ar expression Ns Li }
1175.Pp
1176where
1177.Ar expression
1178consists of all characters until the matching
1179.Ql } .
1180Any
1181.Ql }
1182escaped by a backslash or within a single-quoted string, and characters in
1183embedded arithmetic expansions, command substitutions, and variable
1184expansions, are not examined in determining the matching
1185.Ql } .
1186Except for the variants with
1187.Ql + ,
1188.Ql - ,
1189.Ql =
1190or
1191.Ql ?\& ,
1192any
1193.Ql }
1194within a double-quoted string is also not examined in determining the matching
1195.Ql } .
1196.Pp
1197The simplest form for parameter expansion is:
1198.Pp
1199.D1 Li ${ Ns Ar parameter Ns Li }
1200.Pp
1201The value, if any, of
1202.Ar parameter
1203is substituted.
1204.Pp
1205The parameter name or symbol can be enclosed in braces, which are
1206optional except for positional parameters with more than one digit or
1207when parameter is followed by a character that could be interpreted as
1208part of the name.
1209If a parameter expansion occurs inside double-quotes:
1210.Bl -enum
1211.It
1212Pathname expansion is not performed on the results of the
1213expansion.
1214.It
1215Field splitting is not performed on the results of the
1216expansion, with the exception of the special parameter
1217.Va @ .
1218.El
1219.Pp
1220In addition, a parameter expansion can be modified by using one of the
1221following formats.
1222.Bl -tag -width indent
1223.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li }
1224Use Default Values.
1225If
1226.Ar parameter
1227is unset or null, the expansion of
1228.Ar word
1229is substituted; otherwise, the value of
1230.Ar parameter
1231is substituted.
1232.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li }
1233Assign Default Values.
1234If
1235.Ar parameter
1236is unset or null, the expansion of
1237.Ar word
1238is assigned to
1239.Ar parameter .
1240In all cases, the
1241final value of
1242.Ar parameter
1243is substituted.
1244Quoting inside
1245.Ar word
1246does not prevent field splitting or pathname expansion.
1247Only variables, not positional
1248parameters or special parameters, can be
1249assigned in this way.
1250.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li }
1251Indicate Error if Null or Unset.
1252If
1253.Ar parameter
1254is unset or null, the expansion of
1255.Ar word
1256(or a message indicating it is unset if
1257.Ar word
1258is omitted) is written to standard
1259error and the shell exits with a nonzero
1260exit status.
1261Otherwise, the value of
1262.Ar parameter
1263is substituted.
1264An
1265interactive shell need not exit.
1266.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li }
1267Use Alternate Value.
1268If
1269.Ar parameter
1270is unset or null, null is substituted;
1271otherwise, the expansion of
1272.Ar word
1273is substituted.
1274.El
1275.Pp
1276In the parameter expansions shown previously, use of the colon in the
1277format results in a test for a parameter that is unset or null; omission
1278of the colon results in a test for a parameter that is only unset.
1279.Pp
1280The
1281.Ar word
1282inherits the type of quoting
1283(unquoted, double-quoted or here-document)
1284from the surroundings,
1285with the exception that a backslash that quotes a closing brace is removed
1286during quote removal.
1287.Bl -tag -width indent
1288.It Li ${# Ns Ar parameter Ns Li }
1289String Length.
1290The length in characters of
1291the value of
1292.Ar parameter .
1293.El
1294.Pp
1295The following four varieties of parameter expansion provide for substring
1296processing.
1297In each case, pattern matching notation
1298(see
1299.Sx Shell Patterns ) ,
1300rather than regular expression notation,
1301is used to evaluate the patterns.
1302If parameter is one of the special parameters
1303.Va *
1304or
1305.Va @ ,
1306the result of the expansion is unspecified.
1307Enclosing the full parameter expansion string in double-quotes does not
1308cause the following four varieties of pattern characters to be quoted,
1309whereas quoting characters within the braces has this effect.
1310.Bl -tag -width indent
1311.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li }
1312Remove Smallest Suffix Pattern.
1313The
1314.Ar word
1315is expanded to produce a pattern.
1316The
1317parameter expansion then results in
1318.Ar parameter ,
1319with the smallest portion of the
1320suffix matched by the pattern deleted.
1321.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li }
1322Remove Largest Suffix Pattern.
1323The
1324.Ar word
1325is expanded to produce a pattern.
1326The
1327parameter expansion then results in
1328.Ar parameter ,
1329with the largest portion of the
1330suffix matched by the pattern deleted.
1331.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li }
1332Remove Smallest Prefix Pattern.
1333The
1334.Ar word
1335is expanded to produce a pattern.
1336The
1337parameter expansion then results in
1338.Ar parameter ,
1339with the smallest portion of the
1340prefix matched by the pattern deleted.
1341.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li }
1342Remove Largest Prefix Pattern.
1343The
1344.Ar word
1345is expanded to produce a pattern.
1346The
1347parameter expansion then results in
1348.Ar parameter ,
1349with the largest portion of the
1350prefix matched by the pattern deleted.
1351.El
1352.Ss Command Substitution
1353Command substitution allows the output of a command to be substituted in
1354place of the command name itself.
1355Command substitution occurs when
1356the command is enclosed as follows:
1357.Pp
1358.D1 Li $( Ns Ar command Ns Li )\&
1359.Pp
1360or the backquoted version:
1361.Pp
1362.D1 Li ` Ns Ar command Ns Li `
1363.Pp
1364The shell expands the command substitution by executing command in a
1365subshell environment and replacing the command substitution
1366with the standard output of the command,
1367removing sequences of one or more newlines at the end of the substitution.
1368Embedded newlines before the end of the output are not removed;
1369however, during field splitting, they may be translated into spaces
1370depending on the value of
1371.Va IFS
1372and the quoting that is in effect.
1373.Ss Arithmetic Expansion
1374Arithmetic expansion provides a mechanism for evaluating an arithmetic
1375expression and substituting its value.
1376The format for arithmetic expansion is as follows:
1377.Pp
1378.D1 Li $(( Ns Ar expression Ns Li ))
1379.Pp
1380The
1381.Ar expression
1382is treated as if it were in double-quotes, except
1383that a double-quote inside the expression is not treated specially.
1384The
1385shell expands all tokens in the
1386.Ar expression
1387for parameter expansion,
1388command substitution, and quote removal.
1389.Pp
1390Next, the shell treats this as an arithmetic expression and
1391substitutes the value of the expression.
1392.Ss White Space Splitting (Field Splitting)
1393After parameter expansion, command substitution, and
1394arithmetic expansion the shell scans the results of
1395expansions and substitutions that did not occur in double-quotes for
1396field splitting and multiple fields can result.
1397.Pp
1398The shell treats each character of the
1399.Va IFS
1400variable as a delimiter and uses
1401the delimiters to split the results of parameter expansion and command
1402substitution into fields.
1403.Ss Pathname Expansion (File Name Generation)
1404Unless the
1405.Fl f
1406option is set,
1407file name generation is performed
1408after word splitting is complete.
1409Each word is
1410viewed as a series of patterns, separated by slashes.
1411The
1412process of expansion replaces the word with the names of
1413all existing files whose names can be formed by replacing
1414each pattern with a string that matches the specified pattern.
1415There are two restrictions on this: first, a pattern cannot match
1416a string containing a slash, and second,
1417a pattern cannot match a string starting with a period
1418unless the first character of the pattern is a period.
1419The next section describes the patterns used for both
1420Pathname Expansion and the
1421.Ic case
1422command.
1423.Ss Shell Patterns
1424A pattern consists of normal characters, which match themselves,
1425and meta-characters.
1426The meta-characters are
1427.Ql \&! ,
1428.Ql * ,
1429.Ql \&? ,
1430and
1431.Ql \&[ .
1432These characters lose their special meanings if they are quoted.
1433When command or variable substitution is performed and the dollar sign
1434or back quotes are not double-quoted, the value of the
1435variable or the output of the command is scanned for these
1436characters and they are turned into meta-characters.
1437.Pp
1438An asterisk
1439.Pq Ql *
1440matches any string of characters.
1441A question mark
1442.Pq Ql \&?
1443matches any single character.
1444A left bracket
1445.Pq Ql \&[
1446introduces a character class.
1447The end of the character class is indicated by a
1448.Ql \&] ;
1449if the
1450.Ql \&]
1451is missing then the
1452.Ql \&[
1453matches a
1454.Ql \&[
1455rather than introducing a character class.
1456A character class matches any of the characters between the square brackets.
1457A range of characters may be specified using a minus sign.
1458The character class may be complemented by making an exclamation point
1459.Pq Ql !\&
1460the first character of the character class.
1461.Pp
1462To include a
1463.Ql \&]
1464in a character class, make it the first character listed
1465(after the
1466.Ql \&! ,
1467if any).
1468To include a
1469.Ql - ,
1470make it the first or last character listed.
1471.Ss Built-in Commands
1472This section lists the commands which
1473are built-in because they need to perform some operation
1474that cannot be performed by a separate process.
1475In addition to
1476these, built-in versions of essential utilities
1477are provided for efficiency.
1478.Bl -tag -width indent
1479.It Ic \&:
1480A null command that returns a 0 (true) exit value.
1481.It Ic \&. Ar file
1482The commands in the specified file are read and executed by the shell.
1483The
1484.Ic return
1485command may be used to return to the
1486.Ic \&.
1487command's caller.
1488If
1489.Ar file
1490contains any
1491.Ql /
1492characters, it is used as is.
1493Otherwise, the shell searches the
1494.Ev PATH
1495for the file.
1496If it is not found in the
1497.Ev PATH ,
1498it is sought in the current working directory.
1499.It Ic \&[
1500A built-in equivalent of
1501.Xr test 1 .
1502.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc
1503If
1504.Ar name Ns = Ns Ar string
1505is specified, the shell defines the alias
1506.Ar name
1507with value
1508.Ar string .
1509If just
1510.Ar name
1511is specified, the value of the alias
1512.Ar name
1513is printed.
1514With no arguments, the
1515.Ic alias
1516built-in command prints the names and values of all defined aliases
1517(see
1518.Ic unalias ) .
1519Alias values are written with appropriate quoting so that they are
1520suitable for re-input to the shell.
1521Also see the
1522.Sx Aliases
1523subsection.
1524.It Ic bg Op Ar job ...
1525Continue the specified jobs
1526(or the current job if no jobs are given)
1527in the background.
1528.It Ic builtin Ar cmd Op Ar arg ...
1529Execute the specified built-in command,
1530.Ar cmd .
1531This is useful when the user wishes to override a shell function
1532with the same name as a built-in command.
1533.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
1534List or alter key bindings for the line editor.
1535This command is documented in
1536.Xr editrc 5 .
1537.It Ic cd Oo Fl L | P Oc Op Ar directory
1538Switch to the specified
1539.Ar directory ,
1540or to the directory specified in the
1541.Ev HOME
1542environment variable if no
1543.Ar directory
1544is specified.
1545If
1546.Ar directory
1547does not begin with
1548.Pa / , \&. ,
1549or
1550.Pa .. ,
1551then the directories listed in the
1552.Ev CDPATH
1553variable will be
1554searched for the specified
1555.Ar directory .
1556If
1557.Ev CDPATH
1558is unset, the current directory is searched.
1559The format of
1560.Ev CDPATH
1561is the same as that of
1562.Ev PATH .
1563In an interactive shell,
1564the
1565.Ic cd
1566command will print out the name of the directory
1567that it actually switched to
1568if this is different from the name that the user gave.
1569These may be different either because the
1570.Ev CDPATH
1571mechanism was used or because a symbolic link was crossed.
1572.Pp
1573If the
1574.Fl P
1575option is specified,
1576.Pa ..
1577is handled physically and symbolic links are resolved before
1578.Pa ..
1579components are processed.
1580If the
1581.Fl L
1582option is specified,
1583.Pa ..
1584is handled logically.
1585This is the default.
1586.It Ic chdir
1587A synonym for the
1588.Ic cd
1589built-in command.
1590.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
1591.It Ic command Oo Fl v | V Oc Op Ar utility
1592The first form of invocation executes the specified
1593.Ar utility ,
1594ignoring shell functions in the search.
1595If
1596.Ar utility
1597is a special builtin,
1598it is executed as if it were a regular builtin.
1599.Pp
1600If the
1601.Fl p
1602option is specified, the command search is performed using a
1603default value of
1604.Ev PATH
1605that is guaranteed to find all of the standard utilities.
1606.Pp
1607If the
1608.Fl v
1609option is specified,
1610.Ar utility
1611is not executed but a description of its interpretation by the shell is
1612printed.
1613For ordinary commands the output is the path name; for shell built-in
1614commands, shell functions and keywords only the name is written.
1615Aliases are printed as
1616.Dq Ic alias Ar name Ns = Ns Ar value .
1617.Pp
1618The
1619.Fl V
1620option is identical to
1621.Fl v
1622except for the output.
1623It prints
1624.Dq Ar utility Ic is Ar description
1625where
1626.Ar description
1627is either
1628the path name to
1629.Ar utility ,
1630a special shell builtin,
1631a shell builtin,
1632a shell function,
1633a shell keyword
1634or
1635an alias for
1636.Ar value .
1637.It Ic echo Oo Fl e | n Oc Op Ar string ...
1638Print a space-separated list of the arguments to the standard output
1639and append a newline character.
1640.Bl -tag -width indent
1641.It Fl n
1642Suppress the output of the trailing newline.
1643.It Fl e
1644Process C-style backslash escape sequences.
1645The
1646.Ic echo
1647command understands the following character escapes:
1648.Bl -tag -width indent
1649.It \ea
1650Alert (ring the terminal bell)
1651.It \eb
1652Backspace
1653.It \ec
1654Suppress the trailing newline (this has the side-effect of truncating the
1655line if it is not the last character)
1656.It \ee
1657The ESC character
1658.Tn ( ASCII
16590x1b)
1660.It \ef
1661Formfeed
1662.It \en
1663Newline
1664.It \er
1665Carriage return
1666.It \et
1667Horizontal tab
1668.It \ev
1669Vertical tab
1670.It \e\e
1671Literal backslash
1672.It \e0nnn
1673(Zero) The character whose octal value is
1674.Ar nnn
1675.El
1676.Pp
1677If
1678.Ar string
1679is not enclosed in quotes then the backslash itself must be escaped
1680with a backslash to protect it from the shell.
1681For example
1682.Bd -literal -offset indent
1683$ echo -e "a\evb"
1684a
1685 b
1686$ echo -e a\e\evb
1687a
1688 b
1689$ echo -e "a\e\eb"
1690a\eb
1691$ echo -e a\e\e\e\eb
1692a\eb
1693.Ed
1694.El
1695.Pp
1696Only one of the
1697.Fl e
1698and
1699.Fl n
1700options may be specified.
1701.It Ic eval Ar string ...
1702Concatenate all the arguments with spaces.
1703Then re-parse and execute the command.
1704.It Ic exec Op Ar command Op arg ...
1705Unless
1706.Ar command
1707is omitted,
1708the shell process is replaced with the specified program
1709(which must be a real program, not a shell built-in command or function).
1710Any redirections on the
1711.Ic exec
1712command are marked as permanent,
1713so that they are not undone when the
1714.Ic exec
1715command finishes.
1716.It Ic exit Op Ar exitstatus
1717Terminate the shell process.
1718If
1719.Ar exitstatus
1720is given
1721it is used as the exit status of the shell;
1722otherwise the exit status of the preceding command is used.
1723The exit status should be an integer between 0 and 255.
1724.It Ic export Ar name ...
1725.It Ic export Op Fl p
1726The specified names are exported so that they will
1727appear in the environment of subsequent commands.
1728The only way to un-export a variable is to
1729.Ic unset
1730it.
1731The shell allows the value of a variable to be set
1732at the same time as it is exported by writing
1733.Pp
1734.D1 Ic export Ar name Ns = Ns Ar value
1735.Pp
1736With no arguments the
1737.Ic export
1738command lists the names
1739of all exported variables.
1740If the
1741.Fl p
1742option is specified, the exported variables are printed as
1743.Dq Ic export Ar name Ns = Ns Ar value
1744lines, suitable for re-input to the shell.
1745.It Ic false
1746A null command that returns a non-zero (false) exit value.
1747.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
1748.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
1749.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
1750The
1751.Ic fc
1752built-in command lists, or edits and re-executes,
1753commands previously entered to an interactive shell.
1754.Bl -tag -width indent
1755.It Fl e Ar editor
1756Use the editor named by
1757.Ar editor
1758to edit the commands.
1759The
1760.Ar editor
1761string is a command name,
1762subject to search via the
1763.Ev PATH
1764variable.
1765The value in the
1766.Ev FCEDIT
1767variable is used as a default when
1768.Fl e
1769is not specified.
1770If
1771.Ev FCEDIT
1772is null or unset, the value of the
1773.Ev EDITOR
1774variable is used.
1775If
1776.Ev EDITOR
1777is null or unset,
1778.Xr ed 1
1779is used as the editor.
1780.It Fl l No (ell)
1781List the commands rather than invoking
1782an editor on them.
1783The commands are written in the
1784sequence indicated by the
1785.Ar first
1786and
1787.Ar last
1788operands, as affected by
1789.Fl r ,
1790with each command preceded by the command number.
1791.It Fl n
1792Suppress command numbers when listing with
1793.Fl l .
1794.It Fl r
1795Reverse the order of the commands listed
1796(with
1797.Fl l )
1798or edited
1799(with neither
1800.Fl l
1801nor
1802.Fl s ) .
1803.It Fl s
1804Re-execute the command without invoking an editor.
1805.It Ar first
1806.It Ar last
1807Select the commands to list or edit.
1808The number of previous commands that can be accessed
1809are determined by the value of the
1810.Ev HISTSIZE
1811variable.
1812The value of
1813.Ar first
1814or
1815.Ar last
1816or both are one of the following:
1817.Bl -tag -width indent
1818.It Oo Cm + Oc Ns Ar num
1819A positive number representing a command number;
1820command numbers can be displayed with the
1821.Fl l
1822option.
1823.It Fl Ar num
1824A negative decimal number representing the
1825command that was executed
1826.Ar num
1827of
1828commands previously.
1829For example, \-1 is the immediately previous command.
1830.It Ar string
1831A string indicating the most recently entered command
1832that begins with that string.
1833If the
1834.Ar old Ns = Ns Ar new
1835operand is not also specified with
1836.Fl s ,
1837the string form of the first operand cannot contain an embedded equal sign.
1838.El
1839.El
1840.Pp
1841The following environment variables affect the execution of
1842.Ic fc :
1843.Bl -tag -width ".Ev HISTSIZE"
1844.It Ev FCEDIT
1845Name of the editor to use for history editing.
1846.It Ev HISTSIZE
1847The number of previous commands that are accessible.
1848.El
1849.It Ic fg Op Ar job
1850Move the specified
1851.Ar job
1852or the current job to the foreground.
1853.It Ic getopts Ar optstring var
1854The
1855.Tn POSIX
1856.Ic getopts
1857command.
1858The
1859.Ic getopts
1860command deprecates the older
1861.Xr getopt 1
1862command.
1863The first argument should be a series of letters, each possibly
1864followed by a colon which indicates that the option takes an argument.
1865The specified variable is set to the parsed option.
1866The index of
1867the next argument is placed into the shell variable
1868.Va OPTIND .
1869If an option takes an argument, it is placed into the shell variable
1870.Va OPTARG .
1871If an invalid option is encountered,
1872.Ar var
1873is set to
1874.Ql \&? .
1875It returns a false value (1) when it encounters the end of the options.
1876.It Ic hash Oo Fl rv Oc Op Ar command ...
1877The shell maintains a hash table which remembers the locations of commands.
1878With no arguments whatsoever, the
1879.Ic hash
1880command prints out the contents of this table.
1881Entries which have not been looked at since the last
1882.Ic cd
1883command are marked with an asterisk;
1884it is possible for these entries to be invalid.
1885.Pp
1886With arguments, the
1887.Ic hash
1888command removes each specified
1889.Ar command
1890from the hash table (unless they are functions) and then locates it.
1891With the
1892.Fl v
1893option,
1894.Ic hash
1895prints the locations of the commands as it finds them.
1896The
1897.Fl r
1898option causes the
1899.Ic hash
1900command to delete all the entries in the hash table except for functions.
1901.It Ic jobid Op Ar job
1902Print the process IDs of the processes in the specified
1903.Ar job .
1904If the
1905.Ar job
1906argument is omitted, use the current job.
1907.It Ic jobs Oo Fl lps Oc Op Ar job ...
1908Print information about the specified jobs, or all jobs if no
1909.Ar job
1910argument is given.
1911The information printed includes job ID, status and command name.
1912.Pp
1913If the
1914.Fl l
1915option is specified, the PID of each job is also printed.
1916If the
1917.Fl p
1918option is specified, only the process IDs for the process group leaders
1919are printed, one per line.
1920If the
1921.Fl s
1922option is specified, only the PIDs of the job commands are printed, one per
1923line.
1924.It Ic local Oo Ar variable ... Oc Op Fl
1925See the
1926.Sx Functions
1927subsection.
1928.It Ic pwd Op Fl L | P
1929Print the path of the current directory.
1930The built-in command may
1931differ from the program of the same name because the
1932built-in command remembers what the current directory
1933is rather than recomputing it each time.
1934This makes
1935it faster.
1936However, if the current directory is
1937renamed,
1938the built-in version of
1939.Xr pwd 1
1940will continue to print the old name for the directory.
1941.Pp
1942If the
1943.Fl P
1944option is specified, symbolic links are resolved.
1945If the
1946.Fl L
1947option is specified, the shell's notion of the current directory
1948is printed (symbolic links are not resolved).
1949This is the default.
1950.It Ic read Oo Fl p Ar prompt Oc Oo
1951.Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
1952The
1953.Ar prompt
1954is printed if the
1955.Fl p
1956option is specified
1957and the standard input is a terminal.
1958Then a line is
1959read from the standard input.
1960The trailing newline
1961is deleted from the line and the line is split as
1962described in the section on
1963.Sx White Space Splitting (Field Splitting)
1964above, and
1965the pieces are assigned to the variables in order.
1966If there are more pieces than variables, the remaining
1967pieces (along with the characters in
1968.Va IFS
1969that separated them)
1970are assigned to the last variable.
1971If there are more variables than pieces, the remaining
1972variables are assigned the null string.
1973.Pp
1974Backslashes are treated specially, unless the
1975.Fl r
1976option is
1977specified.
1978If a backslash is followed by
1979a newline, the backslash and the newline will be
1980deleted.
1981If a backslash is followed by any other
1982character, the backslash will be deleted and the following
1983character will be treated as though it were not in
1984.Va IFS ,
1985even if it is.
1986.Pp
1987If the
1988.Fl t
1989option is specified and the
1990.Ar timeout
1991elapses before a complete line of input is supplied,
1992the
1993.Ic read
1994command will return an exit status of 1 without assigning any values.
1995The
1996.Ar timeout
1997value may optionally be followed by one of
1998.Ql s ,
1999.Ql m
2000or
2001.Ql h
2002to explicitly specify seconds, minutes or hours.
2003If none is supplied,
2004.Ql s
2005is assumed.
2006.Pp
2007The
2008.Fl e
2009option exists only for backward compatibility with older scripts.
2010.It Ic readonly Oo Fl p Oc Op Ar name ...
2011Each specified
2012.Ar name
2013is marked as read only,
2014so that it cannot be subsequently modified or unset.
2015The shell allows the value of a variable to be set
2016at the same time as it is marked read only
2017by using the following form:
2018.Pp
2019.D1 Ic readonly Ar name Ns = Ns Ar value
2020.Pp
2021With no arguments the
2022.Ic readonly
2023command lists the names of all read only variables.
2024If the
2025.Fl p
2026option is specified, the read-only variables are printed as
2027.Dq Ic readonly Ar name Ns = Ns Ar value
2028lines, suitable for re-input to the shell.
2029.It Ic return Op Ar exitstatus
2030See the
2031.Sx Functions
2032subsection.
2033.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
2034.Fl c Ar string Oc Op Fl - Ar arg ...
2035The
2036.Ic set
2037command performs three different functions:
2038.Bl -item
2039.It
2040With no arguments, it lists the values of all shell variables.
2041.It
2042If options are given,
2043either in short form or using the long
2044.Dq Fl /+o Ar longname
2045form,
2046it sets or clears the specified options as described in the section called
2047.Sx Argument List Processing .
2048.It
2049If the
2050.Dq Fl -
2051option is specified,
2052.Ic set
2053will replace the shell's positional parameters with the subsequent
2054arguments.
2055If no arguments follow the
2056.Dq Fl -
2057option,
2058all the positional parameters will be cleared,
2059which is equivalent to executing the command
2060.Dq Li "shift $#" .
2061The
2062.Dq Fl -
2063flag may be omitted when specifying arguments to be used
2064as positional replacement parameters.
2065This is not recommended,
2066because the first argument may begin with a dash
2067.Pq Ql -
2068or a plus
2069.Pq Ql + ,
2070which the
2071.Ic set
2072command will interpret as a request to enable or disable options.
2073.El
2074.It Ic setvar Ar variable value
2075Assigns the specified
2076.Ar value
2077to the specified
2078.Ar variable .
2079The
2080.Ic setvar
2081command is intended to be used in functions that
2082assign values to variables whose names are passed as parameters.
2083In general it is better to write
2084.Dq Ar variable Ns = Ns Ar value
2085rather than using
2086.Ic setvar .
2087.It Ic shift Op Ar n
2088Shift the positional parameters
2089.Ar n
2090times, or once if
2091.Ar n
2092is not specified.
2093A shift sets the value of
2094.Li $1
2095to the value of
2096.Li $2 ,
2097the value of
2098.Li $2
2099to the value of
2100.Li $3 ,
2101and so on,
2102decreasing the value of
2103.Li $#
2104by one.
2105If there are zero positional parameters, shifting does not do anything.
2106.It Ic test
2107A built-in equivalent of
2108.Xr test 1 .
2109.It Ic times
2110Print the amount of time spent executing the shell and its children.
2111The first output line shows the user and system times for the shell
2112itself, the second one contains the user and system times for the
2113children.
2114.It Ic trap Oo Ar action Oc Ar signal ...
2115.It Ic trap Fl l
2116Cause the shell to parse and execute
2117.Ar action
2118when any specified
2119.Ar signal
2120is received.
2121The signals are specified by name or number.
2122In addition, the pseudo-signal
2123.Cm EXIT
2124may be used to specify an
2125.Ar action
2126that is performed when the shell terminates.
2127The
2128.Ar action
2129may be an empty string or a dash
2130.Pq Ql - ;
2131the former causes the specified signal to be ignored
2132and the latter causes the default action to be taken.
2133Omitting the
2134.Ar action
2135is another way to request the default action, for compatibility reasons this
2136usage is not recommended though.
2137When the shell forks off a subshell,
2138it resets trapped (but not ignored) signals to the default action.
2139The
2140.Ic trap
2141command has no effect on signals that were ignored on entry to the shell.
2142.Pp
2143Option
2144.Fl l
2145causes the
2146.Ic trap
2147command to display a list of valid signal names.
2148.It Ic true
2149A null command that returns a 0 (true) exit value.
2150.It Ic type Op Ar name ...
2151Interpret each
2152.Ar name
2153as a command and print the resolution of the command search.
2154Possible resolutions are:
2155shell keyword, alias, special shell builtin, shell builtin, command,
2156tracked alias
2157and not found.
2158For aliases the alias expansion is printed;
2159for commands and tracked aliases
2160the complete pathname of the command is printed.
2161.It Ic ulimit Oo Fl HSabcdflmnpstuvw Oc Op Ar limit
2162Set or display resource limits (see
2163.Xr getrlimit 2 ) .
2164If
2165.Ar limit
2166is specified, the named resource will be set;
2167otherwise the current resource value will be displayed.
2168.Pp
2169If
2170.Fl H
2171is specified, the hard limits will be set or displayed.
2172While everybody is allowed to reduce a hard limit,
2173only the superuser can increase it.
2174The
2175.Fl S
2176option
2177specifies the soft limits instead.
2178When displaying limits,
2179only one of
2180.Fl S
2181or
2182.Fl H
2183can be given.
2184The default is to display the soft limits,
2185and to set both the hard and the soft limits.
2186.Pp
2187Option
2188.Fl a
2189causes the
2190.Ic ulimit
2191command to display all resources.
2192The parameter
2193.Ar limit
2194is not acceptable in this mode.
2195.Pp
2196The remaining options specify which resource value is to be
2197displayed or modified.
2198They are mutually exclusive.
2199.Bl -tag -width indent
2200.It Fl b Ar sbsize
2201The maximum size of socket buffer usage, in bytes.
2202.It Fl c Ar coredumpsize
2203The maximal size of core dump files, in 512-byte blocks.
2204.It Fl d Ar datasize
2205The maximal size of the data segment of a process, in kilobytes.
2206.It Fl f Ar filesize
2207The maximal size of a file, in 512-byte blocks.
2208.It Fl l Ar lockedmem
2209The maximal size of memory that can be locked by a process, in
2210kilobytes.
2211.It Fl m Ar memoryuse
2212The maximal resident set size of a process, in kilobytes.
2213.It Fl n Ar nofiles
2214The maximal number of descriptors that could be opened by a process.
2215.It Fl p Ar pseudoterminals
2216The maximal number of pseudo-terminals for this user ID.
2217.It Fl s Ar stacksize
2218The maximal size of the stack segment, in kilobytes.
2219.It Fl t Ar time
2220The maximal amount of CPU time to be used by each process, in seconds.
2221.It Fl u Ar userproc
2222The maximal number of simultaneous processes for this user ID.
2223.It Fl v Ar virtualmem
2224The maximal virtual size of a process, in kilobytes.
2225.It Fl w Ar swapuse
2226The maximum amount of swap space reserved or used for this user ID,
2227in kilobytes.
2228.El
2229.It Ic umask Oo Fl S Oc Op Ar mask
2230Set the file creation mask (see
2231.Xr umask 2 )
2232to the octal or symbolic (see
2233.Xr chmod 1 )
2234value specified by
2235.Ar mask .
2236If the argument is omitted, the current mask value is printed.
2237If the
2238.Fl S
2239option is specified, the output is symbolic, otherwise the output is octal.
2240.It Ic unalias Oo Fl a Oc Op Ar name ...
2241The specified alias names are removed.
2242If
2243.Fl a
2244is specified, all aliases are removed.
2245.It Ic unset Oo Fl fv Oc Ar name ...
2246The specified variables or functions are unset and unexported.
2247If the
2248.Fl v
2249option is specified or no options are given, the
2250.Ar name
2251arguments are treated as variable names.
2252If the
2253.Fl f
2254option is specified, the
2255.Ar name
2256arguments are treated as function names.
2257.It Ic wait Op Ar job
2258Wait for the specified
2259.Ar job
2260to complete and return the exit status of the last process in the
2261.Ar job .
2262If the argument is omitted, wait for all jobs to complete
2263and return an exit status of zero.
2264.El
2265.Ss Commandline Editing
2266When
2267.Nm
2268is being used interactively from a terminal, the current command
2269and the command history
2270(see
2271.Ic fc
2272in
2273.Sx Built-in Commands )
2274can be edited using
2275.Nm vi Ns -mode
2276command line editing.
2277This mode uses commands similar
2278to a subset of those described in the
2279.Xr vi 1
2280man page.
2281The command
2282.Dq Li "set -o vi"
2283(or
2284.Dq Li "set -V" )
2285enables
2286.Nm vi Ns -mode
2287editing and places
2288.Nm
2289into
2290.Nm vi
2291insert mode.
2292With
2293.Nm vi Ns -mode
2294enabled,
2295.Nm
2296can be switched between insert mode and command mode by typing
2297.Aq ESC .
2298Hitting
2299.Aq return
2300while in command mode will pass the line to the shell.
2301.Pp
2302Similarly, the
2303.Dq Li "set -o emacs"
2304(or
2305.Dq Li "set -E" )
2306command can be used to enable a subset of
2307.Nm emacs Ns -style
2308command line editing features.
2309.Sh ENVIRONMENT
2310The following environment variables affect the execution of
2311.Nm :
2312.Bl -tag -width ".Ev HISTSIZE"
2313.It Ev CDPATH
2314The search path used with the
2315.Ic cd
2316built-in.
2317.It Ev EDITOR
2318The fallback editor used with the
2319.Ic fc
2320built-in.
2321If not set, the default editor is
2322.Xr ed 1 .
2323.It Ev FCEDIT
2324The default editor used with the
2325.Ic fc
2326built-in.
2327.It Ev HISTSIZE
2328The number of previous commands that are accessible.
2329.It Ev HOME
2330The starting directory of
2331.Nm .
2332.It Ev IFS
2333Input Field Separators.
2334This is normally set to
2335.Aq space ,
2336.Aq tab ,
2337and
2338.Aq newline .
2339See the
2340.Sx White Space Splitting
2341section for more details.
2342.It Ev MAIL
2343The name of a mail file, that will be checked for the arrival of new
2344mail.
2345Overridden by
2346.Ev MAILPATH .
2347.It Ev MAILPATH
2348A colon
2349.Pq Ql \&:
2350separated list of file names, for the shell to check for incoming
2351mail.
2352This environment setting overrides the
2353.Ev MAIL
2354setting.
2355There is a maximum of 10 mailboxes that can be monitored at once.
2356.It Ev PATH
2357The default search path for executables.
2358See the
2359.Sx Path Search
2360section for details.
2361.It Ev PS1
2362The primary prompt string, which defaults to
2363.Dq Li "$ " ,
2364unless you are the superuser, in which case it defaults to
2365.Dq Li "# " .
2366.It Ev PS2
2367The secondary prompt string, which defaults to
2368.Dq Li "> " .
2369.It Ev PS4
2370The prefix for the trace output (if
2371.Fl x
2372is active).
2373The default is
2374.Dq Li "+ " .
2375.It Ev TERM
2376The default terminal setting for the shell.
2377This is inherited by children of the shell, and is used in the history
2378editing modes.
2379.El
2380.Sh EXIT STATUS
2381Errors that are detected by the shell, such as a syntax error, will
2382cause the shell to exit with a non-zero exit status.
2383If the shell is not an interactive shell, the execution of the shell
2384file will be aborted.
2385Otherwise the shell will return the exit status of the last command
2386executed, or if the
2387.Ic exit
2388builtin is used with a numeric argument, it
2389will return the argument.
2390.Sh SEE ALSO
2391.Xr builtin 1 ,
2392.Xr chsh 1 ,
2393.Xr echo 1 ,
2394.Xr ed 1 ,
2395.Xr emacs 1 ,
2396.Xr expr 1 ,
2397.Xr getopt 1 ,
2398.Xr pwd 1 ,
2399.Xr test 1 ,
2400.Xr vi 1 ,
2401.Xr execve 2 ,
2402.Xr getrlimit 2 ,
2403.Xr umask 2 ,
2404.Xr editrc 5
2405.Sh HISTORY
2406A
2407.Nm
2408command, the Thompson shell, appeared in
2409.At v1 .
2410It was superseded in
2411.At v7
2412by the Bourne shell, which inherited the name
2413.Nm .
2414.Pp
2415This version of
2416.Nm
2417was rewritten in 1989 under the
2418.Bx
2419license after the Bourne shell from
2420.At V.4 .
2421.Sh AUTHORS
2422This version of
2423.Nm
2424was originally written by
2425.An Kenneth Almquist .
2426.Sh BUGS
2427The
2428.Nm
2429utility does not recognize multibyte characters.
2430