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