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