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