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