xref: /freebsd/bin/sh/sh.1 (revision 725f5cdb0dc9696aaaa05b83c4626d4bef21d3fd)
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 May 24, 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 Keywords
419Keywords or reserved 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 keywords:
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 keyword may occur (see above),
433and after checking for keywords, 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 keyword, 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.Va PATH
669variable
670in turn for the command.
671The value of the
672.Va 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 keywords, 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 keyword
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
1043Special parameters are parameters denoted by a single special character
1044or the digit zero.
1045They are shown in the following list, exactly as they would appear in input
1046typed by the user or in the source of a shell script.
1047.Bl -hang
1048.It Li $*
1049Expands to the positional parameters, starting from one.
1050When
1051the expansion occurs within a double-quoted string
1052it expands to a single field with the value of each parameter
1053separated by the first character of the
1054.Va IFS
1055variable,
1056or by a space if
1057.Va IFS
1058is unset.
1059.It Li $@
1060Expands to the positional parameters, starting from one.
1061When
1062the expansion occurs within double-quotes, each positional
1063parameter expands as a separate argument.
1064If there are no positional parameters, the
1065expansion of
1066.Li @
1067generates zero arguments, even when
1068.Li @
1069is double-quoted.
1070What this basically means, for example, is
1071if
1072.Li $1
1073is
1074.Dq Li abc
1075and
1076.Li $2
1077is
1078.Dq Li "def ghi" ,
1079then
1080.Li \&"$@\&"
1081expands to
1082the two arguments:
1083.Bd -literal -offset indent
1084"abc"   "def ghi"
1085.Ed
1086.It Li $#
1087Expands to the number of positional parameters.
1088.It Li $?
1089Expands to the exit status of the most recent pipeline.
1090.It Li $-
1091(hyphen) Expands to the current option flags (the single-letter
1092option names concatenated into a string) as specified on
1093invocation, by the
1094.Ic set
1095built-in command, or implicitly
1096by the shell.
1097.It Li $$
1098Expands to the process ID of the invoked shell.
1099A subshell
1100retains the same value of
1101.Va $
1102as its parent.
1103.It Li $!
1104Expands to the process ID of the most recent background
1105command executed from the current shell.
1106For a
1107pipeline, the process ID is that of the last command in the
1108pipeline.
1109.It Li $0
1110(zero) Expands to the name of the shell or shell script.
1111.El
1112.Ss Special Variables
1113The following variables are set by the shell or
1114have special meaning to it:
1115.Bl -tag -width ".Va HISTSIZE"
1116.It Va CDPATH
1117The search path used with the
1118.Ic cd
1119built-in.
1120.It Va EDITOR
1121The fallback editor used with the
1122.Ic fc
1123built-in.
1124If not set, the default editor is
1125.Xr ed 1 .
1126.It Va FCEDIT
1127The default editor used with the
1128.Ic fc
1129built-in.
1130.It Va HISTSIZE
1131The number of previous commands that are accessible.
1132.It Va HOME
1133The user's home directory,
1134used in tilde expansion and as a default directory for the
1135.Ic cd
1136built-in.
1137.It Va IFS
1138Input Field Separators.
1139This is normally set to
1140.Aq space ,
1141.Aq tab ,
1142and
1143.Aq newline .
1144See the
1145.Sx White Space Splitting
1146section for more details.
1147.It Va LINENO
1148The current line number in the script or function.
1149.It Va MAIL
1150The name of a mail file, that will be checked for the arrival of new
1151mail.
1152Overridden by
1153.Va MAILPATH .
1154.It Va MAILPATH
1155A colon
1156.Pq Ql \&:
1157separated list of file names, for the shell to check for incoming
1158mail.
1159This variable overrides the
1160.Va MAIL
1161setting.
1162There is a maximum of 10 mailboxes that can be monitored at once.
1163.It Va PATH
1164The default search path for executables.
1165See the
1166.Sx Path Search
1167section for details.
1168.It Va PPID
1169The parent process ID of the invoked shell.
1170This is set at startup
1171unless this variable is in the environment.
1172A later change of parent process ID is not reflected.
1173A subshell retains the same value of
1174.Va PPID .
1175.It Va PS1
1176The primary prompt string, which defaults to
1177.Dq Li "$ " ,
1178unless you are the superuser, in which case it defaults to
1179.Dq Li "# " .
1180.It Va PS2
1181The secondary prompt string, which defaults to
1182.Dq Li "> " .
1183.It Va PS4
1184The prefix for the trace output (if
1185.Fl x
1186is active).
1187The default is
1188.Dq Li "+ " .
1189.El
1190.Ss Word Expansions
1191This clause describes the various expansions that are
1192performed on words.
1193Not all expansions are performed on
1194every word, as explained later.
1195.Pp
1196Tilde expansions, parameter expansions, command substitutions,
1197arithmetic expansions, and quote removals that occur within
1198a single word expand to a single field.
1199It is only field
1200splitting or pathname expansion that can create multiple
1201fields from a single word.
1202The single exception to this rule is
1203the expansion of the special parameter
1204.Va @
1205within double-quotes,
1206as was described above.
1207.Pp
1208The order of word expansion is:
1209.Bl -enum
1210.It
1211Tilde Expansion, Parameter Expansion, Command Substitution,
1212Arithmetic Expansion (these all occur at the same time).
1213.It
1214Field Splitting is performed on fields generated by step (1)
1215unless the
1216.Va IFS
1217variable is null.
1218.It
1219Pathname Expansion (unless the
1220.Fl f
1221option is in effect).
1222.It
1223Quote Removal.
1224.El
1225.Pp
1226The
1227.Ql $
1228character is used to introduce parameter expansion, command
1229substitution, or arithmetic evaluation.
1230.Ss Tilde Expansion (substituting a user's home directory)
1231A word beginning with an unquoted tilde character
1232.Pq Ql ~
1233is
1234subjected to tilde expansion.
1235All the characters up to a slash
1236.Pq Ql /
1237or the end of the word are treated as a username
1238and are replaced with the user's home directory.
1239If the
1240username is missing (as in
1241.Pa ~/foobar ) ,
1242the tilde is replaced with the value of the
1243.Va HOME
1244variable (the current user's home directory).
1245.Ss Parameter Expansion
1246The format for parameter expansion is as follows:
1247.Pp
1248.D1 Li ${ Ns Ar expression Ns Li }
1249.Pp
1250where
1251.Ar expression
1252consists of all characters until the matching
1253.Ql } .
1254Any
1255.Ql }
1256escaped by a backslash or within a single-quoted string, and characters in
1257embedded arithmetic expansions, command substitutions, and variable
1258expansions, are not examined in determining the matching
1259.Ql } .
1260Except for the variants with
1261.Ql + ,
1262.Ql - ,
1263.Ql =
1264or
1265.Ql ?\& ,
1266any
1267.Ql }
1268within a double-quoted string is also not examined in determining the matching
1269.Ql } .
1270.Pp
1271The simplest form for parameter expansion is:
1272.Pp
1273.D1 Li ${ Ns Ar parameter Ns Li }
1274.Pp
1275The value, if any, of
1276.Ar parameter
1277is substituted.
1278.Pp
1279The parameter name or symbol can be enclosed in braces, which are
1280optional except for positional parameters with more than one digit or
1281when parameter is followed by a character that could be interpreted as
1282part of the name.
1283If a parameter expansion occurs inside double-quotes:
1284.Bl -enum
1285.It
1286Pathname expansion is not performed on the results of the
1287expansion.
1288.It
1289Field splitting is not performed on the results of the
1290expansion, with the exception of the special parameter
1291.Va @ .
1292.El
1293.Pp
1294In addition, a parameter expansion can be modified by using one of the
1295following formats.
1296.Bl -tag -width indent
1297.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li }
1298Use Default Values.
1299If
1300.Ar parameter
1301is unset or null, the expansion of
1302.Ar word
1303is substituted; otherwise, the value of
1304.Ar parameter
1305is substituted.
1306.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li }
1307Assign Default Values.
1308If
1309.Ar parameter
1310is unset or null, the expansion of
1311.Ar word
1312is assigned to
1313.Ar parameter .
1314In all cases, the
1315final value of
1316.Ar parameter
1317is substituted.
1318Quoting inside
1319.Ar word
1320does not prevent field splitting or pathname expansion.
1321Only variables, not positional
1322parameters or special parameters, can be
1323assigned in this way.
1324.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li }
1325Indicate Error if Null or Unset.
1326If
1327.Ar parameter
1328is unset or null, the expansion of
1329.Ar word
1330(or a message indicating it is unset if
1331.Ar word
1332is omitted) is written to standard
1333error and the shell exits with a nonzero
1334exit status.
1335Otherwise, the value of
1336.Ar parameter
1337is substituted.
1338An
1339interactive shell need not exit.
1340.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li }
1341Use Alternate Value.
1342If
1343.Ar parameter
1344is unset or null, null is substituted;
1345otherwise, the expansion of
1346.Ar word
1347is substituted.
1348.El
1349.Pp
1350In the parameter expansions shown previously, use of the colon in the
1351format results in a test for a parameter that is unset or null; omission
1352of the colon results in a test for a parameter that is only unset.
1353.Pp
1354The
1355.Ar word
1356inherits the type of quoting
1357(unquoted, double-quoted or here-document)
1358from the surroundings,
1359with the exception that a backslash that quotes a closing brace is removed
1360during quote removal.
1361.Bl -tag -width indent
1362.It Li ${# Ns Ar parameter Ns Li }
1363String Length.
1364The length in characters of
1365the value of
1366.Ar parameter .
1367.El
1368.Pp
1369The following four varieties of parameter expansion provide for substring
1370processing.
1371In each case, pattern matching notation
1372(see
1373.Sx Shell Patterns ) ,
1374rather than regular expression notation,
1375is used to evaluate the patterns.
1376If parameter is one of the special parameters
1377.Va *
1378or
1379.Va @ ,
1380the result of the expansion is unspecified.
1381Enclosing the full parameter expansion string in double-quotes does not
1382cause the following four varieties of pattern characters to be quoted,
1383whereas quoting characters within the braces has this effect.
1384.Bl -tag -width indent
1385.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li }
1386Remove Smallest Suffix Pattern.
1387The
1388.Ar word
1389is expanded to produce a pattern.
1390The
1391parameter expansion then results in
1392.Ar parameter ,
1393with the smallest portion of the
1394suffix matched by the pattern deleted.
1395.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li }
1396Remove Largest Suffix Pattern.
1397The
1398.Ar word
1399is expanded to produce a pattern.
1400The
1401parameter expansion then results in
1402.Ar parameter ,
1403with the largest portion of the
1404suffix matched by the pattern deleted.
1405.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li }
1406Remove Smallest Prefix Pattern.
1407The
1408.Ar word
1409is expanded to produce a pattern.
1410The
1411parameter expansion then results in
1412.Ar parameter ,
1413with the smallest portion of the
1414prefix matched by the pattern deleted.
1415.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li }
1416Remove Largest Prefix Pattern.
1417The
1418.Ar word
1419is expanded to produce a pattern.
1420The
1421parameter expansion then results in
1422.Ar parameter ,
1423with the largest portion of the
1424prefix matched by the pattern deleted.
1425.El
1426.Ss Command Substitution
1427Command substitution allows the output of a command to be substituted in
1428place of the command name itself.
1429Command substitution occurs when
1430the command is enclosed as follows:
1431.Pp
1432.D1 Li $( Ns Ar command Ns Li )\&
1433.Pp
1434or the backquoted version:
1435.Pp
1436.D1 Li ` Ns Ar command Ns Li `
1437.Pp
1438The shell expands the command substitution by executing command in a
1439subshell environment and replacing the command substitution
1440with the standard output of the command,
1441removing sequences of one or more newlines at the end of the substitution.
1442Embedded newlines before the end of the output are not removed;
1443however, during field splitting, they may be translated into spaces
1444depending on the value of
1445.Va IFS
1446and the quoting that is in effect.
1447.Ss Arithmetic Expansion
1448Arithmetic expansion provides a mechanism for evaluating an arithmetic
1449expression and substituting its value.
1450The format for arithmetic expansion is as follows:
1451.Pp
1452.D1 Li $(( Ns Ar expression Ns Li ))
1453.Pp
1454The
1455.Ar expression
1456is treated as if it were in double-quotes, except
1457that a double-quote inside the expression is not treated specially.
1458The
1459shell expands all tokens in the
1460.Ar expression
1461for parameter expansion,
1462command substitution, and quote removal.
1463.Pp
1464Next, the shell treats this as an arithmetic expression and
1465substitutes the value of the expression.
1466.Ss White Space Splitting (Field Splitting)
1467After parameter expansion, command substitution, and
1468arithmetic expansion the shell scans the results of
1469expansions and substitutions that did not occur in double-quotes for
1470field splitting and multiple fields can result.
1471.Pp
1472The shell treats each character of the
1473.Va IFS
1474variable as a delimiter and uses
1475the delimiters to split the results of parameter expansion and command
1476substitution into fields.
1477.Ss Pathname Expansion (File Name Generation)
1478Unless the
1479.Fl f
1480option is set,
1481file name generation is performed
1482after word splitting is complete.
1483Each word is
1484viewed as a series of patterns, separated by slashes.
1485The
1486process of expansion replaces the word with the names of
1487all existing files whose names can be formed by replacing
1488each pattern with a string that matches the specified pattern.
1489There are two restrictions on this: first, a pattern cannot match
1490a string containing a slash, and second,
1491a pattern cannot match a string starting with a period
1492unless the first character of the pattern is a period.
1493The next section describes the patterns used for both
1494Pathname Expansion and the
1495.Ic case
1496command.
1497.Ss Shell Patterns
1498A pattern consists of normal characters, which match themselves,
1499and meta-characters.
1500The meta-characters are
1501.Ql \&! ,
1502.Ql * ,
1503.Ql \&? ,
1504and
1505.Ql \&[ .
1506These characters lose their special meanings if they are quoted.
1507When command or variable substitution is performed and the dollar sign
1508or back quotes are not double-quoted, the value of the
1509variable or the output of the command is scanned for these
1510characters and they are turned into meta-characters.
1511.Pp
1512An asterisk
1513.Pq Ql *
1514matches any string of characters.
1515A question mark
1516.Pq Ql \&?
1517matches any single character.
1518A left bracket
1519.Pq Ql \&[
1520introduces a character class.
1521The end of the character class is indicated by a
1522.Ql \&] ;
1523if the
1524.Ql \&]
1525is missing then the
1526.Ql \&[
1527matches a
1528.Ql \&[
1529rather than introducing a character class.
1530A character class matches any of the characters between the square brackets.
1531A range of characters may be specified using a minus sign.
1532The character class may be complemented by making an exclamation point
1533.Pq Ql !\&
1534the first character of the character class.
1535.Pp
1536To include a
1537.Ql \&]
1538in a character class, make it the first character listed
1539(after the
1540.Ql \&! ,
1541if any).
1542To include a
1543.Ql - ,
1544make it the first or last character listed.
1545.Ss Built-in Commands
1546This section lists the commands which
1547are built-in because they need to perform some operation
1548that cannot be performed by a separate process.
1549In addition to
1550these, built-in versions of essential utilities
1551are provided for efficiency.
1552.Bl -tag -width indent
1553.It Ic \&:
1554A null command that returns a 0 (true) exit value.
1555.It Ic \&. Ar file
1556The commands in the specified file are read and executed by the shell.
1557The
1558.Ic return
1559command may be used to return to the
1560.Ic \&.
1561command's caller.
1562If
1563.Ar file
1564contains any
1565.Ql /
1566characters, it is used as is.
1567Otherwise, the shell searches the
1568.Va PATH
1569for the file.
1570If it is not found in the
1571.Va PATH ,
1572it is sought in the current working directory.
1573.It Ic \&[
1574A built-in equivalent of
1575.Xr test 1 .
1576.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc
1577If
1578.Ar name Ns = Ns Ar string
1579is specified, the shell defines the alias
1580.Ar name
1581with value
1582.Ar string .
1583If just
1584.Ar name
1585is specified, the value of the alias
1586.Ar name
1587is printed.
1588With no arguments, the
1589.Ic alias
1590built-in command prints the names and values of all defined aliases
1591(see
1592.Ic unalias ) .
1593Alias values are written with appropriate quoting so that they are
1594suitable for re-input to the shell.
1595Also see the
1596.Sx Aliases
1597subsection.
1598.It Ic bg Op Ar job ...
1599Continue the specified jobs
1600(or the current job if no jobs are given)
1601in the background.
1602.It Ic builtin Ar cmd Op Ar arg ...
1603Execute the specified built-in command,
1604.Ar cmd .
1605This is useful when the user wishes to override a shell function
1606with the same name as a built-in command.
1607.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
1608List or alter key bindings for the line editor.
1609This command is documented in
1610.Xr editrc 5 .
1611.It Ic cd Oo Fl L | P Oc Op Ar directory
1612Switch to the specified
1613.Ar directory ,
1614or to the directory specified in the
1615.Va HOME
1616environment variable if no
1617.Ar directory
1618is specified.
1619If
1620.Ar directory
1621does not begin with
1622.Pa / , \&. ,
1623or
1624.Pa .. ,
1625then the directories listed in the
1626.Va CDPATH
1627variable will be
1628searched for the specified
1629.Ar directory .
1630If
1631.Va CDPATH
1632is unset, the current directory is searched.
1633The format of
1634.Va CDPATH
1635is the same as that of
1636.Va PATH .
1637In an interactive shell,
1638the
1639.Ic cd
1640command will print out the name of the directory
1641that it actually switched to
1642if this is different from the name that the user gave.
1643These may be different either because the
1644.Va CDPATH
1645mechanism was used or because a symbolic link was crossed.
1646.Pp
1647If the
1648.Fl P
1649option is specified,
1650.Pa ..
1651is handled physically and symbolic links are resolved before
1652.Pa ..
1653components are processed.
1654If the
1655.Fl L
1656option is specified,
1657.Pa ..
1658is handled logically.
1659This is the default.
1660.It Ic chdir
1661A synonym for the
1662.Ic cd
1663built-in command.
1664.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
1665.It Ic command Oo Fl v | V Oc Op Ar utility
1666The first form of invocation executes the specified
1667.Ar utility ,
1668ignoring shell functions in the search.
1669If
1670.Ar utility
1671is a special builtin,
1672it is executed as if it were a regular builtin.
1673.Pp
1674If the
1675.Fl p
1676option is specified, the command search is performed using a
1677default value of
1678.Va PATH
1679that is guaranteed to find all of the standard utilities.
1680.Pp
1681If the
1682.Fl v
1683option is specified,
1684.Ar utility
1685is not executed but a description of its interpretation by the shell is
1686printed.
1687For ordinary commands the output is the path name; for shell built-in
1688commands, shell functions and keywords only the name is written.
1689Aliases are printed as
1690.Dq Ic alias Ar name Ns = Ns Ar value .
1691.Pp
1692The
1693.Fl V
1694option is identical to
1695.Fl v
1696except for the output.
1697It prints
1698.Dq Ar utility Ic is Ar description
1699where
1700.Ar description
1701is either
1702the path name to
1703.Ar utility ,
1704a special shell builtin,
1705a shell builtin,
1706a shell function,
1707a shell keyword
1708or
1709an alias for
1710.Ar value .
1711.It Ic echo Oo Fl e | n Oc Op Ar string ...
1712Print a space-separated list of the arguments to the standard output
1713and append a newline character.
1714.Bl -tag -width indent
1715.It Fl n
1716Suppress the output of the trailing newline.
1717.It Fl e
1718Process C-style backslash escape sequences.
1719The
1720.Ic echo
1721command understands the following character escapes:
1722.Bl -tag -width indent
1723.It \ea
1724Alert (ring the terminal bell)
1725.It \eb
1726Backspace
1727.It \ec
1728Suppress the trailing newline (this has the side-effect of truncating the
1729line if it is not the last character)
1730.It \ee
1731The ESC character
1732.Tn ( ASCII
17330x1b)
1734.It \ef
1735Formfeed
1736.It \en
1737Newline
1738.It \er
1739Carriage return
1740.It \et
1741Horizontal tab
1742.It \ev
1743Vertical tab
1744.It \e\e
1745Literal backslash
1746.It \e0nnn
1747(Zero) The character whose octal value is
1748.Ar nnn
1749.El
1750.Pp
1751If
1752.Ar string
1753is not enclosed in quotes then the backslash itself must be escaped
1754with a backslash to protect it from the shell.
1755For example
1756.Bd -literal -offset indent
1757$ echo -e "a\evb"
1758a
1759 b
1760$ echo -e a\e\evb
1761a
1762 b
1763$ echo -e "a\e\eb"
1764a\eb
1765$ echo -e a\e\e\e\eb
1766a\eb
1767.Ed
1768.El
1769.Pp
1770Only one of the
1771.Fl e
1772and
1773.Fl n
1774options may be specified.
1775.It Ic eval Ar string ...
1776Concatenate all the arguments with spaces.
1777Then re-parse and execute the command.
1778.It Ic exec Op Ar command Op arg ...
1779Unless
1780.Ar command
1781is omitted,
1782the shell process is replaced with the specified program
1783(which must be a real program, not a shell built-in command or function).
1784Any redirections on the
1785.Ic exec
1786command are marked as permanent,
1787so that they are not undone when the
1788.Ic exec
1789command finishes.
1790.It Ic exit Op Ar exitstatus
1791Terminate the shell process.
1792If
1793.Ar exitstatus
1794is given
1795it is used as the exit status of the shell;
1796otherwise the exit status of the preceding command is used.
1797The exit status should be an integer between 0 and 255.
1798.It Ic export Ar name ...
1799.It Ic export Op Fl p
1800The specified names are exported so that they will
1801appear in the environment of subsequent commands.
1802The only way to un-export a variable is to
1803.Ic unset
1804it.
1805The shell allows the value of a variable to be set
1806at the same time as it is exported by writing
1807.Pp
1808.D1 Ic export Ar name Ns = Ns Ar value
1809.Pp
1810With no arguments the
1811.Ic export
1812command lists the names
1813of all exported variables.
1814If the
1815.Fl p
1816option is specified, the exported variables are printed as
1817.Dq Ic export Ar name Ns = Ns Ar value
1818lines, suitable for re-input to the shell.
1819.It Ic false
1820A null command that returns a non-zero (false) exit value.
1821.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
1822.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
1823.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
1824The
1825.Ic fc
1826built-in command lists, or edits and re-executes,
1827commands previously entered to an interactive shell.
1828.Bl -tag -width indent
1829.It Fl e Ar editor
1830Use the editor named by
1831.Ar editor
1832to edit the commands.
1833The
1834.Ar editor
1835string is a command name,
1836subject to search via the
1837.Va PATH
1838variable.
1839The value in the
1840.Va FCEDIT
1841variable is used as a default when
1842.Fl e
1843is not specified.
1844If
1845.Va FCEDIT
1846is null or unset, the value of the
1847.Va EDITOR
1848variable is used.
1849If
1850.Va EDITOR
1851is null or unset,
1852.Xr ed 1
1853is used as the editor.
1854.It Fl l No (ell)
1855List the commands rather than invoking
1856an editor on them.
1857The commands are written in the
1858sequence indicated by the
1859.Ar first
1860and
1861.Ar last
1862operands, as affected by
1863.Fl r ,
1864with each command preceded by the command number.
1865.It Fl n
1866Suppress command numbers when listing with
1867.Fl l .
1868.It Fl r
1869Reverse the order of the commands listed
1870(with
1871.Fl l )
1872or edited
1873(with neither
1874.Fl l
1875nor
1876.Fl s ) .
1877.It Fl s
1878Re-execute the command without invoking an editor.
1879.It Ar first
1880.It Ar last
1881Select the commands to list or edit.
1882The number of previous commands that can be accessed
1883are determined by the value of the
1884.Va HISTSIZE
1885variable.
1886The value of
1887.Ar first
1888or
1889.Ar last
1890or both are one of the following:
1891.Bl -tag -width indent
1892.It Oo Cm + Oc Ns Ar num
1893A positive number representing a command number;
1894command numbers can be displayed with the
1895.Fl l
1896option.
1897.It Fl Ar num
1898A negative decimal number representing the
1899command that was executed
1900.Ar num
1901of
1902commands previously.
1903For example, \-1 is the immediately previous command.
1904.It Ar string
1905A string indicating the most recently entered command
1906that begins with that string.
1907If the
1908.Ar old Ns = Ns Ar new
1909operand is not also specified with
1910.Fl s ,
1911the string form of the first operand cannot contain an embedded equal sign.
1912.El
1913.El
1914.Pp
1915The following variables affect the execution of
1916.Ic fc :
1917.Bl -tag -width ".Va HISTSIZE"
1918.It Va FCEDIT
1919Name of the editor to use for history editing.
1920.It Va HISTSIZE
1921The number of previous commands that are accessible.
1922.El
1923.It Ic fg Op Ar job
1924Move the specified
1925.Ar job
1926or the current job to the foreground.
1927.It Ic getopts Ar optstring var
1928The
1929.Tn POSIX
1930.Ic getopts
1931command.
1932The
1933.Ic getopts
1934command deprecates the older
1935.Xr getopt 1
1936command.
1937The first argument should be a series of letters, each possibly
1938followed by a colon which indicates that the option takes an argument.
1939The specified variable is set to the parsed option.
1940The index of
1941the next argument is placed into the shell variable
1942.Va OPTIND .
1943If an option takes an argument, it is placed into the shell variable
1944.Va OPTARG .
1945If an invalid option is encountered,
1946.Ar var
1947is set to
1948.Ql \&? .
1949It returns a false value (1) when it encounters the end of the options.
1950.It Ic hash Oo Fl rv Oc Op Ar command ...
1951The shell maintains a hash table which remembers the locations of commands.
1952With no arguments whatsoever, the
1953.Ic hash
1954command prints out the contents of this table.
1955Entries which have not been looked at since the last
1956.Ic cd
1957command are marked with an asterisk;
1958it is possible for these entries to be invalid.
1959.Pp
1960With arguments, the
1961.Ic hash
1962command removes each specified
1963.Ar command
1964from the hash table (unless they are functions) and then locates it.
1965With the
1966.Fl v
1967option,
1968.Ic hash
1969prints the locations of the commands as it finds them.
1970The
1971.Fl r
1972option causes the
1973.Ic hash
1974command to delete all the entries in the hash table except for functions.
1975.It Ic jobid Op Ar job
1976Print the process IDs of the processes in the specified
1977.Ar job .
1978If the
1979.Ar job
1980argument is omitted, use the current job.
1981.It Ic jobs Oo Fl lps Oc Op Ar job ...
1982Print information about the specified jobs, or all jobs if no
1983.Ar job
1984argument is given.
1985The information printed includes job ID, status and command name.
1986.Pp
1987If the
1988.Fl l
1989option is specified, the PID of each job is also printed.
1990If the
1991.Fl p
1992option is specified, only the process IDs for the process group leaders
1993are printed, one per line.
1994If the
1995.Fl s
1996option is specified, only the PIDs of the job commands are printed, one per
1997line.
1998.It Ic local Oo Ar variable ... Oc Op Fl
1999See the
2000.Sx Functions
2001subsection.
2002.It Ic pwd Op Fl L | P
2003Print the path of the current directory.
2004The built-in command may
2005differ from the program of the same name because the
2006built-in command remembers what the current directory
2007is rather than recomputing it each time.
2008This makes
2009it faster.
2010However, if the current directory is
2011renamed,
2012the built-in version of
2013.Xr pwd 1
2014will continue to print the old name for the directory.
2015.Pp
2016If the
2017.Fl P
2018option is specified, symbolic links are resolved.
2019If the
2020.Fl L
2021option is specified, the shell's notion of the current directory
2022is printed (symbolic links are not resolved).
2023This is the default.
2024.It Ic read Oo Fl p Ar prompt Oc Oo
2025.Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
2026The
2027.Ar prompt
2028is printed if the
2029.Fl p
2030option is specified
2031and the standard input is a terminal.
2032Then a line is
2033read from the standard input.
2034The trailing newline
2035is deleted from the line and the line is split as
2036described in the section on
2037.Sx White Space Splitting (Field Splitting)
2038above, and
2039the pieces are assigned to the variables in order.
2040If there are more pieces than variables, the remaining
2041pieces (along with the characters in
2042.Va IFS
2043that separated them)
2044are assigned to the last variable.
2045If there are more variables than pieces, the remaining
2046variables are assigned the null string.
2047.Pp
2048Backslashes are treated specially, unless the
2049.Fl r
2050option is
2051specified.
2052If a backslash is followed by
2053a newline, the backslash and the newline will be
2054deleted.
2055If a backslash is followed by any other
2056character, the backslash will be deleted and the following
2057character will be treated as though it were not in
2058.Va IFS ,
2059even if it is.
2060.Pp
2061If the
2062.Fl t
2063option is specified and the
2064.Ar timeout
2065elapses before a complete line of input is supplied,
2066the
2067.Ic read
2068command will return an exit status of 1 without assigning any values.
2069The
2070.Ar timeout
2071value may optionally be followed by one of
2072.Ql s ,
2073.Ql m
2074or
2075.Ql h
2076to explicitly specify seconds, minutes or hours.
2077If none is supplied,
2078.Ql s
2079is assumed.
2080.Pp
2081The
2082.Fl e
2083option exists only for backward compatibility with older scripts.
2084.It Ic readonly Oo Fl p Oc Op Ar name ...
2085Each specified
2086.Ar name
2087is marked as read only,
2088so that it cannot be subsequently modified or unset.
2089The shell allows the value of a variable to be set
2090at the same time as it is marked read only
2091by using the following form:
2092.Pp
2093.D1 Ic readonly Ar name Ns = Ns Ar value
2094.Pp
2095With no arguments the
2096.Ic readonly
2097command lists the names of all read only variables.
2098If the
2099.Fl p
2100option is specified, the read-only variables are printed as
2101.Dq Ic readonly Ar name Ns = Ns Ar value
2102lines, suitable for re-input to the shell.
2103.It Ic return Op Ar exitstatus
2104See the
2105.Sx Functions
2106subsection.
2107.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
2108.Fl c Ar string Oc Op Fl - Ar arg ...
2109The
2110.Ic set
2111command performs three different functions:
2112.Bl -item
2113.It
2114With no arguments, it lists the values of all shell variables.
2115.It
2116If options are given,
2117either in short form or using the long
2118.Dq Fl /+o Ar longname
2119form,
2120it sets or clears the specified options as described in the section called
2121.Sx Argument List Processing .
2122.It
2123If the
2124.Dq Fl -
2125option is specified,
2126.Ic set
2127will replace the shell's positional parameters with the subsequent
2128arguments.
2129If no arguments follow the
2130.Dq Fl -
2131option,
2132all the positional parameters will be cleared,
2133which is equivalent to executing the command
2134.Dq Li "shift $#" .
2135The
2136.Dq Fl -
2137flag may be omitted when specifying arguments to be used
2138as positional replacement parameters.
2139This is not recommended,
2140because the first argument may begin with a dash
2141.Pq Ql -
2142or a plus
2143.Pq Ql + ,
2144which the
2145.Ic set
2146command will interpret as a request to enable or disable options.
2147.El
2148.It Ic setvar Ar variable value
2149Assigns the specified
2150.Ar value
2151to the specified
2152.Ar variable .
2153The
2154.Ic setvar
2155command is intended to be used in functions that
2156assign values to variables whose names are passed as parameters.
2157In general it is better to write
2158.Dq Ar variable Ns = Ns Ar value
2159rather than using
2160.Ic setvar .
2161.It Ic shift Op Ar n
2162Shift the positional parameters
2163.Ar n
2164times, or once if
2165.Ar n
2166is not specified.
2167A shift sets the value of
2168.Li $1
2169to the value of
2170.Li $2 ,
2171the value of
2172.Li $2
2173to the value of
2174.Li $3 ,
2175and so on,
2176decreasing the value of
2177.Li $#
2178by one.
2179If there are zero positional parameters, shifting does not do anything.
2180.It Ic test
2181A built-in equivalent of
2182.Xr test 1 .
2183.It Ic times
2184Print the amount of time spent executing the shell and its children.
2185The first output line shows the user and system times for the shell
2186itself, the second one contains the user and system times for the
2187children.
2188.It Ic trap Oo Ar action Oc Ar signal ...
2189.It Ic trap Fl l
2190Cause the shell to parse and execute
2191.Ar action
2192when any specified
2193.Ar signal
2194is received.
2195The signals are specified by name or number.
2196In addition, the pseudo-signal
2197.Cm EXIT
2198may be used to specify an
2199.Ar action
2200that is performed when the shell terminates.
2201The
2202.Ar action
2203may be an empty string or a dash
2204.Pq Ql - ;
2205the former causes the specified signal to be ignored
2206and the latter causes the default action to be taken.
2207Omitting the
2208.Ar action
2209is another way to request the default action, for compatibility reasons this
2210usage is not recommended though.
2211When the shell forks off a subshell,
2212it resets trapped (but not ignored) signals to the default action.
2213The
2214.Ic trap
2215command has no effect on signals that were ignored on entry to the shell.
2216.Pp
2217Option
2218.Fl l
2219causes the
2220.Ic trap
2221command to display a list of valid signal names.
2222.It Ic true
2223A null command that returns a 0 (true) exit value.
2224.It Ic type Op Ar name ...
2225Interpret each
2226.Ar name
2227as a command and print the resolution of the command search.
2228Possible resolutions are:
2229shell keyword, alias, special shell builtin, shell builtin, command,
2230tracked alias
2231and not found.
2232For aliases the alias expansion is printed;
2233for commands and tracked aliases
2234the complete pathname of the command is printed.
2235.It Ic ulimit Oo Fl HSabcdflmnpstuvw Oc Op Ar limit
2236Set or display resource limits (see
2237.Xr getrlimit 2 ) .
2238If
2239.Ar limit
2240is specified, the named resource will be set;
2241otherwise the current resource value will be displayed.
2242.Pp
2243If
2244.Fl H
2245is specified, the hard limits will be set or displayed.
2246While everybody is allowed to reduce a hard limit,
2247only the superuser can increase it.
2248The
2249.Fl S
2250option
2251specifies the soft limits instead.
2252When displaying limits,
2253only one of
2254.Fl S
2255or
2256.Fl H
2257can be given.
2258The default is to display the soft limits,
2259and to set both the hard and the soft limits.
2260.Pp
2261Option
2262.Fl a
2263causes the
2264.Ic ulimit
2265command to display all resources.
2266The parameter
2267.Ar limit
2268is not acceptable in this mode.
2269.Pp
2270The remaining options specify which resource value is to be
2271displayed or modified.
2272They are mutually exclusive.
2273.Bl -tag -width indent
2274.It Fl b Ar sbsize
2275The maximum size of socket buffer usage, in bytes.
2276.It Fl c Ar coredumpsize
2277The maximal size of core dump files, in 512-byte blocks.
2278.It Fl d Ar datasize
2279The maximal size of the data segment of a process, in kilobytes.
2280.It Fl f Ar filesize
2281The maximal size of a file, in 512-byte blocks.
2282.It Fl l Ar lockedmem
2283The maximal size of memory that can be locked by a process, in
2284kilobytes.
2285.It Fl m Ar memoryuse
2286The maximal resident set size of a process, in kilobytes.
2287.It Fl n Ar nofiles
2288The maximal number of descriptors that could be opened by a process.
2289.It Fl p Ar pseudoterminals
2290The maximal number of pseudo-terminals for this user ID.
2291.It Fl s Ar stacksize
2292The maximal size of the stack segment, in kilobytes.
2293.It Fl t Ar time
2294The maximal amount of CPU time to be used by each process, in seconds.
2295.It Fl u Ar userproc
2296The maximal number of simultaneous processes for this user ID.
2297.It Fl v Ar virtualmem
2298The maximal virtual size of a process, in kilobytes.
2299.It Fl w Ar swapuse
2300The maximum amount of swap space reserved or used for this user ID,
2301in kilobytes.
2302.El
2303.It Ic umask Oo Fl S Oc Op Ar mask
2304Set the file creation mask (see
2305.Xr umask 2 )
2306to the octal or symbolic (see
2307.Xr chmod 1 )
2308value specified by
2309.Ar mask .
2310If the argument is omitted, the current mask value is printed.
2311If the
2312.Fl S
2313option is specified, the output is symbolic, otherwise the output is octal.
2314.It Ic unalias Oo Fl a Oc Op Ar name ...
2315The specified alias names are removed.
2316If
2317.Fl a
2318is specified, all aliases are removed.
2319.It Ic unset Oo Fl fv Oc Ar name ...
2320The specified variables or functions are unset and unexported.
2321If the
2322.Fl v
2323option is specified or no options are given, the
2324.Ar name
2325arguments are treated as variable names.
2326If the
2327.Fl f
2328option is specified, the
2329.Ar name
2330arguments are treated as function names.
2331.It Ic wait Op Ar job
2332Wait for the specified
2333.Ar job
2334to complete and return the exit status of the last process in the
2335.Ar job .
2336If the argument is omitted, wait for all jobs to complete
2337and return an exit status of zero.
2338.El
2339.Ss Commandline Editing
2340When
2341.Nm
2342is being used interactively from a terminal, the current command
2343and the command history
2344(see
2345.Ic fc
2346in
2347.Sx Built-in Commands )
2348can be edited using
2349.Nm vi Ns -mode
2350command line editing.
2351This mode uses commands similar
2352to a subset of those described in the
2353.Xr vi 1
2354man page.
2355The command
2356.Dq Li "set -o vi"
2357(or
2358.Dq Li "set -V" )
2359enables
2360.Nm vi Ns -mode
2361editing and places
2362.Nm
2363into
2364.Nm vi
2365insert mode.
2366With
2367.Nm vi Ns -mode
2368enabled,
2369.Nm
2370can be switched between insert mode and command mode by typing
2371.Aq ESC .
2372Hitting
2373.Aq return
2374while in command mode will pass the line to the shell.
2375.Pp
2376Similarly, the
2377.Dq Li "set -o emacs"
2378(or
2379.Dq Li "set -E" )
2380command can be used to enable a subset of
2381.Nm emacs Ns -style
2382command line editing features.
2383.Sh ENVIRONMENT
2384The following environment variables affect the execution of
2385.Nm :
2386.Bl -tag -width ".Ev LANGXXXXXX"
2387.It Ev ENV
2388Initialization file for interactive shells.
2389.It Ev LANG , Ev LC_*
2390Locale settings.
2391These are inherited by children of the shell,
2392and is used in a limited manner by the shell itself.
2393.It Ev PWD
2394An absolute pathname for the current directory,
2395possibly containing symbolic links.
2396This is used and updated by the shell.
2397.It Ev TERM
2398The default terminal setting for the shell.
2399This is inherited by children of the shell, and is used in the history
2400editing modes.
2401.El
2402.Pp
2403Additionally, all environment variables are turned into shell variables
2404at startup,
2405which may affect the shell as described under
2406.Sx Special Variables .
2407.Sh EXIT STATUS
2408Errors that are detected by the shell, such as a syntax error, will
2409cause the shell to exit with a non-zero exit status.
2410If the shell is not an interactive shell, the execution of the shell
2411file will be aborted.
2412Otherwise the shell will return the exit status of the last command
2413executed, or if the
2414.Ic exit
2415builtin is used with a numeric argument, it
2416will return the argument.
2417.Sh SEE ALSO
2418.Xr builtin 1 ,
2419.Xr chsh 1 ,
2420.Xr echo 1 ,
2421.Xr ed 1 ,
2422.Xr emacs 1 ,
2423.Xr expr 1 ,
2424.Xr getopt 1 ,
2425.Xr pwd 1 ,
2426.Xr test 1 ,
2427.Xr vi 1 ,
2428.Xr execve 2 ,
2429.Xr getrlimit 2 ,
2430.Xr umask 2 ,
2431.Xr editrc 5
2432.Sh HISTORY
2433A
2434.Nm
2435command, the Thompson shell, appeared in
2436.At v1 .
2437It was superseded in
2438.At v7
2439by the Bourne shell, which inherited the name
2440.Nm .
2441.Pp
2442This version of
2443.Nm
2444was rewritten in 1989 under the
2445.Bx
2446license after the Bourne shell from
2447.At V.4 .
2448.Sh AUTHORS
2449This version of
2450.Nm
2451was originally written by
2452.An Kenneth Almquist .
2453.Sh BUGS
2454The
2455.Nm
2456utility does not recognize multibyte characters.
2457