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