xref: /freebsd/bin/sh/sh.1 (revision bd15659f64eb4460b85f395a176de9d4fcf40ecb)
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 July 3, 2004
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 /+abCEefIimnPpsTuVvx
44.Op Fl /+o Ar longname
45.Op Fl c Ar string
46.Op Ar arg ...
47.Sh DESCRIPTION
48The
49.Nm
50utility is the standard command interpreter for the system.
51The current version of
52.Nm
53is in the process of being changed to
54conform with the
55.St -p1003.2
56specification for the shell.
57This version has many features which make
58it appear
59similar in some respects to the Korn shell, but it is not a Korn
60shell clone like pdksh.
61Only features
62designated by POSIX, plus a few Berkeley extensions, are being
63incorporated into this shell.
64This man page is not intended to be a tutorial nor a complete
65specification of the shell.
66.Ss Overview
67The shell is a command that reads lines from
68either a file or the terminal, interprets them, and
69generally executes other commands.
70It is the program that is started when a user logs into the system,
71although a user can select a different shell with the
72.Xr chsh 1
73command.
74The shell
75implements a language that has flow control constructs,
76a macro facility that provides a variety of features in
77addition to data storage, along with built-in history and line
78editing capabilities.
79It incorporates many features to
80aid interactive use and has the advantage that the interpretative
81language is common to both interactive and non-interactive
82use (shell scripts).
83That is, commands can be typed directly
84to the running shell or can be put into a file,
85which can be executed directly by the shell.
86.Ss Invocation
87.\"
88.\" XXX This next sentence is incredibly confusing.
89.\"
90If no arguments are present and if the standard input of the shell
91is connected to a terminal
92(or if the
93.Fl i
94option is set),
95the shell is considered an interactive shell.
96An interactive shell
97generally prompts before each command and handles programming
98and command errors differently (as described below).
99When first starting, the shell inspects argument 0, and
100if it begins with a dash
101.Pq Li - ,
102the shell is also considered a login shell.
103This is normally done automatically by the system
104when the user first logs in.
105A login shell first reads commands
106from the files
107.Pa /etc/profile
108and then
109.Pa .profile
110if they exist.
111If the environment variable
112.Ev ENV
113is set on entry to a shell, or is set in the
114.Pa .profile
115of a login shell, the shell then reads commands from the file named in
116.Ev ENV .
117Therefore, a user should place commands that are to be executed only
118at login time in the
119.Pa .profile
120file, and commands that are executed for every shell inside the
121.Ev ENV
122file.
123The user can set the
124.Ev ENV
125variable to some file by placing the following line in the file
126.Pa .profile
127in the home directory,
128substituting for
129.Pa .shinit
130the filename desired:
131.Pp
132.Dl ENV=$HOME/.shinit; export ENV
133.Pp
134The first non-option argument specified on the command line
135will be treated as the
136name of a file from which to read commands (a shell script), and
137the remaining arguments are set as the positional parameters
138of the shell ($1, $2, etc).
139Otherwise, the shell reads commands
140from its standard input.
141.Pp
142Unlike older versions of
143.Nm
144the
145.Ev ENV
146script is only sourced on invocation of interactive shells.
147This
148closes a well-known, and sometimes easily exploitable security
149hole related to poorly thought out
150.Ev ENV
151scripts.
152.Ss Argument List Processing
153All of the single letter options to
154.Nm
155have a corresponding long name,
156with the exception of
157.Fl c
158and
159.Fl /+o .
160These long names are provided next to the single letter options
161in the descriptions below.
162The long name for an option may be specified as an argument to the
163.Fl /+o
164option of
165.Nm .
166Once the shell is running,
167the long name for an option may be specified as an argument to the
168.Fl /+o
169option of the
170.Ic set
171built-in command
172(described later in the section called
173.Sx Built-in Commands ) .
174Introducing an option with a dash
175.Pq Li -
176enables the option,
177while using a plus
178.Pq Li +
179disables the option.
180A
181.Dq Li --
182or plain
183.Dq Li -
184will stop option processing and will force the remaining
185words on the command line to be treated as arguments.
186The
187.Fl /+o
188and
189.Fl c
190options do not have long names.
191They take arguments and are described after the single letter options.
192.Bl -tag -width indent
193.It Fl a Li allexport
194Flag variables for export when assignments are made to them.
195.It Fl b Li notify
196Enable asynchronous notification of background job
197completion.
198(UNIMPLEMENTED)
199.It Fl C Li noclobber
200Do not overwrite existing files with
201.Dq Li > .
202.It Fl E Li emacs
203Enable the built-in
204.Xr emacs 1
205command line editor (disables the
206.Fl V
207option if it has been set).
208.It Fl e Li errexit
209Exit immediately if any untested command fails in non-interactive mode.
210The exit status of a command is considered to be
211explicitly tested if the command is used to control
212an if, elif, while, or until; or if the command is the left
213hand operand of an
214.Dq Li &&
215or
216.Dq Li ||
217operator.
218.It Fl f Li noglob
219Disable pathname expansion.
220.It Fl I Li ignoreeof
221Ignore
222.Dv EOF Ns ' Ns s
223from input when in interactive mode.
224.It Fl i Li interactive
225Force the shell to behave interactively.
226.It Fl m Li monitor
227Turn on job control (set automatically when interactive).
228.It Fl n Li noexec
229If not interactive, read commands but do not
230execute them.
231This is useful for checking the
232syntax of shell scripts.
233.It Fl P Li physical
234Change the default for the
235.Ic cd
236and
237.Ic pwd
238commands from
239.Fl L
240(logical directory layout)
241to
242.Fl P
243(physical directory layout).
244.It Fl p Li privileged
245Turn on privileged mode.
246This mode is enabled on startup
247if either the effective user or group id is not equal to the
248real user or group id.
249Turning this mode off sets the
250effective user and group ids to the real user and group ids.
251When this mode is enabled for interactive shells, the file
252.Pa /etc/suid_profile
253is sourced instead of
254.Pa ~/.profile
255after
256.Pa /etc/profile
257is sourced, and the contents of the
258.Ev ENV
259variable are ignored.
260.It Fl s Li stdin
261Read commands from standard input (set automatically
262if no file arguments are present).
263This option has
264no effect when set after the shell has already started
265running (i.e., when set with the
266.Ic set
267command).
268.It Fl T Li trapsasync
269When waiting for a child, execute traps immediately.
270If this option is not set,
271traps are executed after the child exits,
272as specified in
273.St -p1003.2 .
274This nonstandard option is useful for putting guarding shells around
275children that block signals.
276The surrounding shell may kill the child
277or it may just return control to the tty and leave the child alone,
278like this:
279.Bd -literal -offset indent
280sh -T -c "trap 'exit 1' 2 ; some-blocking-program"
281.Ed
282.Pp
283.It Fl u Li nounset
284Write a message to standard error when attempting
285to expand a variable that is not set, and if the
286shell is not interactive, exit immediately.
287.It Fl V Li vi
288Enable the built-in
289.Xr vi 1
290command line editor (disables
291.Fl E
292if it has been set).
293.It Fl v Li verbose
294The shell writes its input to standard error
295as it is read.
296Useful for debugging.
297.It Fl x Li xtrace
298Write each command
299(preceded by
300.Dq Li "+ " )
301to standard error before it is executed.
302Useful for debugging.
303.El
304.Pp
305The
306.Fl c
307option may be used to pass its string argument to the shell
308to be interpreted as input.
309Keep in mind that this option only accepts a single string as its
310argument, hence multi-word strings must be quoted.
311.Pp
312The
313.Fl /+o
314option takes as its only argument the long name of an option
315to be enabled or disabled.
316For example, the following two invocations of
317.Nm
318both enable the built-in
319.Xr emacs 1
320command line editor:
321.Bd -literal -offset indent
322set -E
323set -o emacs
324.Ed
325.Pp
326If used without an argument, the
327.Fl o
328option displays the current option settings in a human-readable format.
329If
330.Cm +o
331is used without an argument, the current option settings are output
332in a format suitable for re-input into the shell.
333.Ss Lexical Structure
334The shell reads input in terms of lines from a file and breaks
335it up into words at whitespace (blanks and tabs), and at
336certain sequences of
337characters called
338.Dq operators ,
339which are special to the shell.
340There are two types of operators: control operators and
341redirection operators (their meaning is discussed later).
342The following is a list of valid operators:
343.Bl -tag -width indent
344.It Control operators:
345.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
346.It Li & Ta Li && Ta Li ( Ta Li ) Ta Li \en
347.It Li ;; Ta Li ; Ta Li | Ta Li ||
348.El
349.It Redirection operators:
350.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
351.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <>
352.It Li <& Ta Li >& Ta Li <<- Ta Li >|
353.El
354.El
355.Ss Quoting
356Quoting is used to remove the special meaning of certain characters
357or words to the shell, such as operators, whitespace, or
358keywords.
359There are three types of quoting: matched single quotes,
360matched double quotes, and backslash.
361.Bl -tag -width indent
362.It Single Quotes
363Enclosing characters in single quotes preserves the literal
364meaning of all the characters (except single quotes, making
365it impossible to put single-quotes in a single-quoted string).
366.It Double Quotes
367Enclosing characters within double quotes preserves the literal
368meaning of all characters except dollarsign
369.Pq Li $ ,
370backquote
371.Pq Li ` ,
372and backslash
373.Pq Li \e .
374The backslash inside double quotes is historically weird.
375It remains literal unless it precedes the following characters,
376which it serves to quote:
377.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
378.It Li $ Ta Li ` Ta Li \&" Ta Li \e\  Ta Li \en
379.El
380.It Backslash
381A backslash preserves the literal meaning of the following
382character, with the exception of the newline character
383.Pq Li \en .
384A backslash preceding a newline is treated as a line continuation.
385.El
386.Ss Reserved Words
387Reserved words are words that have special meaning to the
388shell and are recognized at the beginning of a line and
389after a control operator.
390The following are reserved words:
391.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
392.It Li \&! Ta { Ta } Ta Ic case Ta Ic do
393.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
394.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
395.El
396.Ss Aliases
397An alias is a name and corresponding value set using the
398.Ic alias
399built-in command.
400Whenever a reserved word may occur (see above),
401and after checking for reserved words, the shell
402checks the word to see if it matches an alias.
403If it does, it replaces it in the input stream with its value.
404For example, if there is an alias called
405.Dq Li lf
406with the value
407.Dq Li ls -F ,
408then the input
409.Bd -literal -offset indent
410lf foobar
411.Ed
412.Pp
413would become
414.Bd -literal -offset indent
415ls -F foobar
416.Ed
417.Pp
418Aliases provide a convenient way for naive users to
419create shorthands for commands without having to learn how
420to create functions with arguments.
421They can also be
422used to create lexically obscure code.
423This use is discouraged.
424.Ss Commands
425The shell interprets the words it reads according to a
426language, the specification of which is outside the scope
427of this man page (refer to the BNF in the
428.St -p1003.2
429document).
430Essentially though, a line is read and if
431the first word of the line (or after a control operator)
432is not a reserved word, then the shell has recognized a
433simple command.
434Otherwise, a complex command or some
435other special construct may have been recognized.
436.Ss Simple Commands
437If a simple command has been recognized, the shell performs
438the following actions:
439.Bl -enum
440.It
441Leading words of the form
442.Dq Li name=value
443are stripped off and assigned to the environment of
444the simple command.
445Redirection operators and
446their arguments (as described below) are stripped
447off and saved for processing.
448.It
449The remaining words are expanded as described in
450the section called
451.Sx Word Expansions ,
452and the first remaining word is considered the command
453name and the command is located.
454The remaining
455words are considered the arguments of the command.
456If no command name resulted, then the
457.Dq Li name=value
458variable assignments recognized in 1) affect the
459current shell.
460.It
461Redirections are performed as described in
462the next section.
463.El
464.Ss Redirections
465Redirections are used to change where a command reads its input
466or sends its output.
467In general, redirections open, close, or
468duplicate an existing reference to a file.
469The overall format
470used for redirection is:
471.Pp
472.Dl [n] redir-op file
473.Pp
474The
475.Ql redir-op
476is one of the redirection operators mentioned
477previously.
478The following gives some examples of how these
479operators can be used.
480Note that stdin and stdout are commonly used abbreviations
481for standard input and standard output respectively.
482.Bl -tag -width "1234567890XX" -offset indent
483.It Li [n]> file
484redirect stdout (or file descriptor n) to file
485.It Li [n]>| file
486same as above, but override the
487.Fl C
488option
489.It Li [n]>> file
490append stdout (or file descriptor n) to file
491.It Li [n]< file
492redirect stdin (or file descriptor n) from file
493.It Li [n]<> file
494redirect stdin (or file descriptor n) to and from file
495.It Li [n1]<&n2
496duplicate stdin (or file descriptor n1) from file descriptor n2
497.It Li [n]<&-
498close stdin (or file descriptor n)
499.It Li [n1]>&n2
500duplicate stdout (or file descriptor n1) to file descriptor n2
501.It Li [n]>&-
502close stdout (or file descriptor n)
503.El
504.Pp
505The following redirection is often called a
506.Dq here-document .
507.Bd -literal -offset indent
508[n]<< delimiter
509	here-doc-text
510	...
511delimiter
512.Ed
513.Pp
514All the text on successive lines up to the delimiter is
515saved away and made available to the command on standard
516input, or file descriptor n if it is specified.
517If the delimiter
518as specified on the initial line is quoted, then the here-doc-text
519is treated literally, otherwise the text is subjected to
520parameter expansion, command substitution, and arithmetic
521expansion (as described in the section on
522.Sx Word Expansions ) .
523If the operator is
524.Dq Li <<-
525instead of
526.Dq Li << ,
527then leading tabs
528in the here-doc-text are stripped.
529.Ss Search and Execution
530There are three types of commands: shell functions,
531built-in commands, and normal programs.
532The command is searched for (by name) in that order.
533The three types of commands are all executed in a different way.
534.Pp
535When a shell function is executed, all of the shell positional
536parameters (except $0, which remains unchanged) are
537set to the arguments of the shell function.
538The variables which are explicitly placed in the environment of
539the command (by placing assignments to them before the
540function name) are made local to the function and are set
541to the values given.
542Then the command given in the function definition is executed.
543The positional parameters are restored to their original values
544when the command completes.
545This all occurs within the current shell.
546.Pp
547Shell built-in commands are executed internally to the shell, without
548spawning a new process.
549.Pp
550Otherwise, if the command name does not match a function
551or built-in command, the command is searched for as a normal
552program in the file system (as described in the next section).
553When a normal program is executed, the shell runs the program,
554passing the arguments and the environment to the program.
555If the program is not a normal executable file
556(i.e., if it does not begin with the
557.Qq magic number
558whose
559.Tn ASCII
560representation is
561.Qq #! ,
562resulting in an
563.Er ENOEXEC
564return value from
565.Xr execve 2 )
566the shell will interpret the program in a subshell.
567The child shell will reinitialize itself in this case,
568so that the effect will be
569as if a new shell had been invoked to handle the ad-hoc shell script,
570except that the location of hashed commands located in
571the parent shell will be remembered by the child.
572.Pp
573Note that previous versions of this document
574and the source code itself misleadingly and sporadically
575refer to a shell script without a magic number
576as a
577.Qq shell procedure .
578.Ss Path Search
579When locating a command, the shell first looks to see if
580it has a shell function by that name.
581Then it looks for a
582built-in command by that name.
583If a built-in command is not found,
584one of two things happen:
585.Bl -enum
586.It
587Command names containing a slash are simply executed without
588performing any searches.
589.It
590The shell searches each entry in
591.Ev PATH
592in turn for the command.
593The value of the
594.Ev PATH
595variable should be a series of
596entries separated by colons.
597Each entry consists of a
598directory name.
599The current directory
600may be indicated implicitly by an empty directory name,
601or explicitly by a single period.
602.El
603.Ss Command Exit Status
604Each command has an exit status that can influence the behavior
605of other shell commands.
606The paradigm is that a command exits
607with zero for normal or success, and non-zero for failure,
608error, or a false indication.
609The man page for each command
610should indicate the various exit codes and what they mean.
611Additionally, the built-in commands return exit codes, as does
612an executed shell function.
613.Pp
614If a command is terminated by a signal, its exit status is 128 plus
615the signal number.
616Signal numbers are defined in the header file
617.In sys/signal.h .
618.Ss Complex Commands
619Complex commands are combinations of simple commands
620with control operators or reserved words, together creating a larger complex
621command.
622More generally, a command is one of the following:
623.Bl -item -offset indent
624.It
625simple command
626.It
627pipeline
628.It
629list or compound-list
630.It
631compound command
632.It
633function definition
634.El
635.Pp
636Unless otherwise stated, the exit status of a command is
637that of the last simple command executed by the command.
638.Ss Pipelines
639A pipeline is a sequence of one or more commands separated
640by the control operator |.
641The standard output of all but
642the last command is connected to the standard input
643of the next command.
644The standard output of the last
645command is inherited from the shell, as usual.
646.Pp
647The format for a pipeline is:
648.Pp
649.Dl [!] command1 [ | command2 ...]
650.Pp
651The standard output of command1 is connected to the standard
652input of command2.
653The standard input, standard output, or
654both of a command is considered to be assigned by the
655pipeline before any redirection specified by redirection
656operators that are part of the command.
657.Pp
658If the pipeline is not in the background (discussed later),
659the shell waits for all commands to complete.
660.Pp
661If the reserved word
662.Ic !\&
663does not precede the pipeline, the
664exit status is the exit status of the last command specified
665in the pipeline.
666Otherwise, the exit status is the logical
667NOT of the exit status of the last command.
668That is, if
669the last command returns zero, the exit status is 1; if
670the last command returns greater than zero, the exit status
671is zero.
672.Pp
673Because pipeline assignment of standard input or standard
674output or both takes place before redirection, it can be
675modified by redirection.
676For example:
677.Pp
678.Dl $ command1 2>&1 | command2
679.Pp
680sends both the standard output and standard error of
681.Ql command1
682to the standard input of
683.Ql command2 .
684.Pp
685A
686.Dq Li \&;
687or newline terminator causes the preceding
688AND-OR-list
689(described below in the section called
690.Sx Short-Circuit List Operators )
691to be executed sequentially;
692an
693.Dq Li &
694causes asynchronous execution of the preceding AND-OR-list.
695.Pp
696Note that unlike some other shells,
697.Nm
698executes each process in the pipeline as a child of the
699.Nm
700process.
701Shell built-in commands are the exception to this rule.
702They are executed in the current shell, although they do not affect its
703environment when used in pipelines.
704.Ss Background Commands (&)
705If a command is terminated by the control operator ampersand
706.Pq Li & ,
707the shell executes the command asynchronously;
708the shell does not wait for the command to finish
709before executing the next command.
710.Pp
711The format for running a command in background is:
712.Bd -literal -offset indent
713command1 & [command2 & ...]
714.Ed
715.Pp
716If the shell is not interactive, the standard input of an
717asynchronous command is set to /dev/null.
718.Ss Lists (Generally Speaking)
719A list is a sequence of zero or more commands separated by
720newlines, semicolons, or ampersands,
721and optionally terminated by one of these three characters.
722The commands in a
723list are executed in the order they are written.
724If command is followed by an ampersand, the shell starts the
725command and immediately proceeds onto the next command;
726otherwise it waits for the command to terminate before
727proceeding to the next one.
728.Ss Short-Circuit List Operators
729.Dq Li &&
730and
731.Dq Li ||
732are AND-OR list operators.
733.Dq Li &&
734executes the first command, and then executes the second command
735if the exit status of the first command is zero.
736.Dq Li ||
737is similar, but executes the second command if the exit
738status of the first command is nonzero.
739.Dq Li &&
740and
741.Dq Li ||
742both have the same priority.
743.Ss Flow-Control Constructs (if, while, for, case)
744The syntax of the
745.Ic if
746command is:
747.\"
748.\" XXX Use .Dl to work around broken handling of .Ic inside .Bd and .Ed .
749.\"
750.Dl Ic if Ar list
751.Dl Ic then Ar list
752.Dl [ Ic elif Ar list
753.Dl Ic then Ar list ] ...
754.Dl [ Ic else Ar list ]
755.Dl Ic fi
756.Pp
757The syntax of the
758.Ic while
759command is:
760.Dl Ic while Ar list
761.Dl Ic do Ar list
762.Dl Ic done
763.Pp
764The two lists are executed repeatedly while the exit status of the
765first list is zero.
766The
767.Ic until
768command is similar, but has the word
769.Ic until
770in place of
771.Ic while ,
772which causes it to
773repeat until the exit status of the first list is zero.
774.Pp
775The syntax of the
776.Ic for
777command is:
778.Dl Ic for Ar variable Ic in Ar word ...
779.Dl Ic do Ar list
780.Dl Ic done
781.Pp
782The words are expanded, and then the list is executed
783repeatedly with the variable set to each word in turn.
784The
785.Ic do
786and
787.Ic done
788commands may be replaced with
789.Dq Li {
790and
791.Dq Li } .
792.Pp
793The syntax of the
794.Ic break
795and
796.Ic continue
797commands is:
798.Dl Ic break Op Ar num
799.Dl Ic continue Op Ar num
800.Pp
801The
802.Ic break
803command terminates the
804.Ar num
805innermost
806.Ic for
807or
808.Ic while
809loops.
810The
811.Ic continue
812command continues with the next iteration of the innermost loop.
813These are implemented as built-in commands.
814.Pp
815The syntax of the
816.Ic case
817command is
818.Dl Ic case Ar word Ic in
819.Dl pattern) list ;;
820.Dl ...
821.Dl Ic esac
822.Pp
823The pattern can actually be one or more patterns
824(see
825.Sx Shell Patterns
826described later),
827separated by
828.Dq Li \&|
829characters.
830.Ss Grouping Commands Together
831Commands may be grouped by writing either
832.Bd -literal -offset indent
833(list)
834.Ed
835.Pp
836or
837.Bd -literal -offset indent
838{ list; }
839.Ed
840.Pp
841The first form executes the commands in a subshell.
842Note that built-in commands thus executed do not affect the current shell.
843The second form does not fork another shell,
844so it is slightly more efficient.
845Grouping commands together this way allows the user to
846redirect their output as though they were one program:
847.Bd -literal -offset indent
848{ echo -n "hello"; echo " world"; } > greeting
849.Ed
850.Ss Functions
851The syntax of a function definition is
852.Bd -literal -offset indent
853name ( ) command
854.Ed
855.Pp
856A function definition is an executable statement; when
857executed it installs a function named name and returns an
858exit status of zero.
859The command is normally a list
860enclosed between
861.Dq Li {
862and
863.Dq Li } .
864.Pp
865Variables may be declared to be local to a function by
866using the
867.Ic local
868command.
869This should appear as the first statement of a function,
870and the syntax is:
871.Bd -ragged -offset indent
872.Ic local
873.Op Ar variable ...
874.Op Ar -
875.Ed
876.Pp
877The
878.Ic local
879command is implemented as a built-in command.
880.Pp
881When a variable is made local, it inherits the initial
882value and exported and readonly flags from the variable
883with the same name in the surrounding scope, if there is
884one.
885Otherwise, the variable is initially unset.
886The shell
887uses dynamic scoping, so that if the variable
888.Em x
889is made local to function
890.Em f ,
891which then calls function
892.Em g ,
893references to the variable
894.Em x
895made inside
896.Em g
897will refer to the variable
898.Em x
899declared inside
900.Em f ,
901not to the global variable named
902.Em x .
903.Pp
904The only special parameter that can be made local is
905.Dq Li - .
906Making
907.Dq Li -
908local causes any shell options that are
909changed via the set command inside the function to be
910restored to their original values when the function
911returns.
912.Pp
913The syntax of the
914.Ic return
915command is
916.Bd -ragged -offset indent
917.Ic return
918.Op Ar exitstatus
919.Ed
920.Pp
921It terminates the current executional scope, returning from the previous
922nested function, sourced script, or shell instance, in that order.
923The
924.Ic return
925command is implemented as a built-in command.
926.Ss Variables and Parameters
927The shell maintains a set of parameters.
928A parameter
929denoted by a name is called a variable.
930When starting up,
931the shell turns all the environment variables into shell
932variables.
933New variables can be set using the form
934.Bd -literal -offset indent
935name=value
936.Ed
937.Pp
938Variables set by the user must have a name consisting solely
939of alphabetics, numerics, and underscores.
940The first letter of a variable name must not be numeric.
941A parameter can also be denoted by a number
942or a special character as explained below.
943.Ss Positional Parameters
944A positional parameter is a parameter denoted by a number greater than zero.
945The shell sets these initially to the values of its command line
946arguments that follow the name of the shell script.
947The
948.Ic set
949built-in command can also be used to set or reset them.
950.Ss Special Parameters
951A special parameter is a parameter denoted by a special one-character
952name.
953The special parameters recognized by the
954.Nm
955shell of
956.Fx
957are shown in the following list, exactly as they would appear in input
958typed by the user or in the source of a shell script.
959.Bl -hang
960.It Li $*
961Expands to the positional parameters, starting from one.
962When
963the expansion occurs within a double-quoted string
964it expands to a single field with the value of each parameter
965separated by the first character of the
966.Ev IFS
967variable,
968or by a
969.Aq space
970if
971.Ev IFS
972is unset.
973.It Li $@
974Expands to the positional parameters, starting from one.
975When
976the expansion occurs within double-quotes, each positional
977parameter expands as a separate argument.
978If there are no positional parameters, the
979expansion of
980.Li @
981generates zero arguments, even when
982.Li @
983is double-quoted.
984What this basically means, for example, is
985if $1 is
986.Dq abc
987and $2 is
988.Dq def ghi ,
989then
990.Qq Li $@
991expands to
992the two arguments:
993.Bd -literal -offset indent
994"abc"   "def ghi"
995.Ed
996.It Li $#
997Expands to the number of positional parameters.
998.It Li $\&?
999Expands to the exit status of the most recent pipeline.
1000.It Li $-
1001(hyphen) Expands to the current option flags (the single-letter
1002option names concatenated into a string) as specified on
1003invocation, by the set built-in command, or implicitly
1004by the shell.
1005.It Li $$
1006Expands to the process ID of the invoked shell.
1007A subshell
1008retains the same value of $ as its parent.
1009.It Li $\&!
1010Expands to the process ID of the most recent background
1011command executed from the current shell.
1012For a
1013pipeline, the process ID is that of the last command in the
1014pipeline.
1015.It Li $0
1016(zero) Expands to the name of the shell or shell script.
1017.El
1018.Ss Word Expansions
1019This clause describes the various expansions that are
1020performed on words.
1021Not all expansions are performed on
1022every word, as explained later.
1023.Pp
1024Tilde expansions, parameter expansions, command substitutions,
1025arithmetic expansions, and quote removals that occur within
1026a single word expand to a single field.
1027It is only field
1028splitting or pathname expansion that can create multiple
1029fields from a single word.
1030The single exception to this rule is
1031the expansion of the special parameter
1032.Li @
1033within double-quotes,
1034as was described above.
1035.Pp
1036The order of word expansion is:
1037.Bl -enum
1038.It
1039Tilde Expansion, Parameter Expansion, Command Substitution,
1040Arithmetic Expansion (these all occur at the same time).
1041.It
1042Field Splitting is performed on fields generated by step (1)
1043unless the
1044.Ev IFS
1045variable is null.
1046.It
1047Pathname Expansion (unless the
1048.Fl f
1049option is in effect).
1050.It
1051Quote Removal.
1052.El
1053.Pp
1054The
1055.Dq Li $
1056character is used to introduce parameter expansion, command
1057substitution, or arithmetic evaluation.
1058.Ss Tilde Expansion (substituting a user's home directory)
1059A word beginning with an unquoted tilde character
1060.Pq Li ~
1061is
1062subjected to tilde expansion.
1063All the characters up to a slash
1064.Pq Li /
1065or the end of the word are treated as a username
1066and are replaced with the user's home directory.
1067If the
1068username is missing (as in ~/foobar), the tilde is replaced
1069with the value of the HOME variable (the current user's
1070home directory).
1071.Ss Parameter Expansion
1072The format for parameter expansion is as follows:
1073.Bd -literal -offset indent
1074${expression}
1075.Ed
1076.Pp
1077where expression consists of all characters until the matching
1078.Dq Li } .
1079Any
1080.Dq Li }
1081escaped by a backslash or within a quoted string, and characters in
1082embedded arithmetic expansions, command substitutions, and variable
1083expansions, are not examined in determining the matching
1084.Dq Li } .
1085.Pp
1086The simplest form for parameter expansion is:
1087.Bd -literal -offset indent
1088${parameter}
1089.Ed
1090.Pp
1091The value, if any, of parameter is substituted.
1092.Pp
1093The parameter name or symbol can be enclosed in braces, which are
1094optional except for positional parameters with more than one digit or
1095when parameter is followed by a character that could be interpreted as
1096part of the name.
1097If a parameter expansion occurs inside double-quotes:
1098.Bl -enum
1099.It
1100Pathname expansion is not performed on the results of the
1101expansion.
1102.It
1103Field splitting is not performed on the results of the
1104expansion, with the exception of the special parameter
1105.Li @ .
1106.El
1107.Pp
1108In addition, a parameter expansion can be modified by using one of the
1109following formats.
1110.Bl -tag -width indent
1111.It Li ${parameter:-word}
1112Use Default Values.
1113If parameter is unset or
1114null, the expansion of word is
1115substituted; otherwise, the value of
1116parameter is substituted.
1117.It Li ${parameter:=word}
1118Assign Default Values.
1119If parameter is unset
1120or null, the expansion of word is
1121assigned to parameter.
1122In all cases, the
1123final value of parameter is
1124substituted.
1125Only variables, not positional
1126parameters or special parameters, can be
1127assigned in this way.
1128.It Li ${parameter:?[word]}
1129Indicate Error if Null or Unset.
1130If
1131parameter is unset or null, the expansion of
1132word (or a message indicating it is unset if
1133word is omitted) is written to standard
1134error and the shell exits with a nonzero
1135exit status.
1136Otherwise, the value of
1137parameter is substituted.
1138An
1139interactive shell need not exit.
1140.It Li ${parameter:+word}
1141Use Alternate Value.
1142If parameter is unset
1143or null, null is substituted;
1144otherwise, the expansion of word is
1145substituted.
1146.El
1147.Pp
1148In the parameter expansions shown previously, use of the colon in the
1149format results in a test for a parameter that is unset or null; omission
1150of the colon results in a test for a parameter that is only unset.
1151.Bl -tag -width indent
1152.It Li ${#parameter}
1153String Length.
1154The length in characters of
1155the value of parameter.
1156.El
1157.Pp
1158The following four varieties of parameter expansion provide for substring
1159processing.
1160In each case, pattern matching notation
1161(see
1162.Sx Shell Patterns ) ,
1163rather than regular expression notation,
1164is used to evaluate the patterns.
1165If parameter is one of the special parameters
1166.Li *
1167or
1168.Li @ ,
1169the result of the expansion is unspecified.
1170Enclosing the full parameter expansion string in double-quotes does not
1171cause the following four varieties of pattern characters to be quoted,
1172whereas quoting characters within the braces has this effect.
1173.Bl -tag -width indent
1174.It Li ${parameter%word}
1175Remove Smallest Suffix Pattern.
1176The word
1177is expanded to produce a pattern.
1178The
1179parameter expansion then results in
1180parameter, with the smallest portion of the
1181suffix matched by the pattern deleted.
1182.It Li ${parameter%%word}
1183Remove Largest Suffix Pattern.
1184The word
1185is expanded to produce a pattern.
1186The
1187parameter expansion then results in
1188parameter, with the largest portion of the
1189suffix matched by the pattern deleted.
1190.It Li ${parameter#word}
1191Remove Smallest Prefix Pattern.
1192The word
1193is expanded to produce a pattern.
1194The
1195parameter expansion then results in
1196parameter, with the smallest portion of the
1197prefix matched by the pattern deleted.
1198.It Li ${parameter##word}
1199Remove Largest Prefix Pattern.
1200The word
1201is expanded to produce a pattern.
1202The
1203parameter expansion then results in
1204parameter, with the largest portion of the
1205prefix matched by the pattern deleted.
1206.El
1207.Ss Command Substitution
1208Command substitution allows the output of a command to be substituted in
1209place of the command name itself.
1210Command substitution occurs when
1211the command is enclosed as follows:
1212.Bd -literal -offset indent
1213$(command)
1214.Ed
1215.Pp
1216or the backquoted version:
1217.Bd -literal -offset indent
1218`command`
1219.Ed
1220.Pp
1221The shell expands the command substitution by executing command in a
1222subshell environment and replacing the command substitution
1223with the standard output of the command,
1224removing sequences of one or more newlines at the end of the substitution.
1225Embedded newlines before the end of the output are not removed;
1226however, during field splitting, they may be translated into spaces
1227depending on the value of
1228.Ev IFS
1229and the quoting that is in effect.
1230.Ss Arithmetic Expansion
1231Arithmetic expansion provides a mechanism for evaluating an arithmetic
1232expression and substituting its value.
1233The format for arithmetic expansion is as follows:
1234.Bd -literal -offset indent
1235$((expression))
1236.Ed
1237.Pp
1238The expression is treated as if it were in double-quotes, except
1239that a double-quote inside the expression is not treated specially.
1240The
1241shell expands all tokens in the expression for parameter expansion,
1242command substitution, and quote removal.
1243.Pp
1244Next, the shell treats this as an arithmetic expression and
1245substitutes the value of the expression.
1246.Ss White Space Splitting (Field Splitting)
1247After parameter expansion, command substitution, and
1248arithmetic expansion the shell scans the results of
1249expansions and substitutions that did not occur in double-quotes for
1250field splitting and multiple fields can result.
1251.Pp
1252The shell treats each character of the
1253.Ev IFS
1254as a delimiter and uses
1255the delimiters to split the results of parameter expansion and command
1256substitution into fields.
1257.Ss Pathname Expansion (File Name Generation)
1258Unless the
1259.Fl f
1260option is set,
1261file name generation is performed
1262after word splitting is complete.
1263Each word is
1264viewed as a series of patterns, separated by slashes.
1265The
1266process of expansion replaces the word with the names of
1267all existing files whose names can be formed by replacing
1268each pattern with a string that matches the specified pattern.
1269There are two restrictions on this: first, a pattern cannot match
1270a string containing a slash, and second,
1271a pattern cannot match a string starting with a period
1272unless the first character of the pattern is a period.
1273The next section describes the patterns used for both
1274Pathname Expansion and the
1275.Ic case
1276command.
1277.Ss Shell Patterns
1278A pattern consists of normal characters, which match themselves,
1279and meta-characters.
1280The meta-characters are
1281.Dq Li \&! ,
1282.Dq Li * ,
1283.Dq Li \&? ,
1284and
1285.Dq Li [ .
1286These characters lose their special meanings if they are quoted.
1287When command or variable substitution is performed and the dollar sign
1288or back quotes are not double-quoted, the value of the
1289variable or the output of the command is scanned for these
1290characters and they are turned into meta-characters.
1291.Pp
1292An asterisk
1293.Pq Li *
1294matches any string of characters.
1295A question mark
1296.Pq Li \&?
1297matches any single character.
1298A left bracket
1299.Pq Li [
1300introduces a character class.
1301The end of the character class is indicated by a
1302.Dq Li \&] ;
1303if the
1304.Dq Li \&]
1305is missing then the
1306.Dq Li [
1307matches a
1308.Dq Li [
1309rather than introducing a character class.
1310A character class matches any of the characters between the square brackets.
1311A range of characters may be specified using a minus sign.
1312The character class may be complemented by making an exclamation point
1313.Pq Li !\&
1314the first character of the character class.
1315.Pp
1316To include a
1317.Dq Li \&]
1318in a character class, make it the first character listed
1319(after the
1320.Dq Li \&! ,
1321if any).
1322To include a
1323.Dq Li - ,
1324make it the first or last character listed.
1325.Ss Built-in Commands
1326This section lists the commands which
1327are built-in because they need to perform some operation
1328that cannot be performed by a separate process.
1329In addition to
1330these, a built-in version of the
1331.Xr test 1
1332command is provided for efficiency.
1333.Bl -tag -width indent
1334.It Ic \&:
1335A null command that returns a 0 (true) exit value.
1336.It Ic \&. Ar file
1337The commands in the specified file are read and executed by the shell.
1338The
1339.Ic return
1340command may be used to return to the
1341.Ic \&.
1342command's caller.
1343If
1344.Ar file
1345contains any
1346.Dq /
1347characters, it is used as is.
1348Otherwise, the shell searches the
1349.Ev PATH
1350for the file.
1351If it is not found in the
1352.Ev PATH ,
1353it is sought in the current working directory.
1354.It Ic alias Op Ar name ...
1355.It Ic alias Op Ar name Ns = Ns Ar string ...
1356If
1357.Ar name Ns = Ns Ar string
1358is specified, the shell defines the alias
1359.Ar name
1360with value
1361.Ar string .
1362If just
1363.Ar name
1364is specified, the value of the alias
1365.Ar name
1366is printed.
1367With no arguments, the
1368.Ic alias
1369built-in command prints the names and values of all defined aliases
1370(see
1371.Ic unalias ) .
1372Alias values are written with appropriate quoting so that they are
1373suitable for re-input to the shell.
1374.It Ic bg Op Ar job ...
1375Continue the specified jobs
1376(or the current job if no jobs are given)
1377in the background.
1378.It Ic builtin Ar cmd Op Ar arg ...
1379Execute the specified built-in command,
1380.Ar cmd .
1381This is useful when the user wishes to override a shell function
1382with the same name as a built-in command.
1383.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
1384List or alter key bindings for the line editor.
1385This command is documented in
1386.Xr editrc 5 .
1387.It Ic cd Oo Fl L | P Oc Op Ar directory
1388Switch to the specified
1389.Ar directory ,
1390or to the directory specified in the
1391.Ev HOME
1392environment variable if no
1393.Ar directory
1394is specified.
1395If
1396.Ar directory
1397does not begin with
1398.Pa / , \&. ,
1399or
1400.Pa .. ,
1401then the directories listed in the
1402.Ev CDPATH
1403variable will be
1404searched for the specified
1405.Ar directory .
1406If
1407.Ev CDPATH
1408is unset, the current directory is searched.
1409The format of
1410.Ar CDPATH
1411is the same as that of
1412.Ev PATH .
1413In an interactive shell,
1414the
1415.Ic cd
1416command will print out the name of the directory
1417that it actually switched to
1418if this is different from the name that the user gave.
1419These may be different either because the
1420.Ev CDPATH
1421mechanism was used or because a symbolic link was crossed.
1422.Pp
1423If the
1424.Fl P
1425option is specified,
1426.Pa ..
1427is handled physically and symbolic links are resolved before
1428.Pa ..
1429components are processed.
1430If the
1431.Fl L
1432option is specified,
1433.Pa ..
1434is handled logically.
1435This is the default.
1436.It Ic chdir
1437A synonym for the
1438.Ic cd
1439built-in command.
1440.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
1441Execute the specified
1442.Ar utility
1443as a simple command (see the
1444.Sx Simple Commands
1445section).
1446.Pp
1447If the
1448.Fl p
1449option is specified, the command search is performed using a
1450default value of
1451.Ev PATH
1452that is guaranteed to find all of the standard utilities.
1453.It Ic echo Oo Fl e | n Oc Op Ar string
1454Print
1455.Ar string
1456to the standard output with a newline appended.
1457.Bl -tag -width indent
1458.It Fl n
1459Suppress the output of the trailing newline.
1460.It Fl e
1461Process C-style backslash escape sequences.
1462.Ic echo
1463understands the following character escapes:
1464.Bl -tag -width indent
1465.It \ea
1466Alert (ring the terminal bell)
1467.It \eb
1468Backspace
1469.It \ec
1470Suppress the trailing newline (this has the side-effect of truncating the
1471line if it is not the last character)
1472.It \ee
1473The ESC character (ASCII 0x1b)
1474.It \ef
1475Formfeed
1476.It \en
1477Newline
1478.It \er
1479Carriage return
1480.It \et
1481Horizontal tab
1482.It \ev
1483Vertical tab
1484.It \e\e
1485Literal backslash
1486.It \e0nnn
1487(Zero) The character whose octal value is nnn
1488.El
1489.Pp
1490If
1491.Ar string
1492is not enclosed in quotes then the backslash itself must be escaped
1493with a backslash to protect it from the shell.
1494For example
1495.Bd -literal -offset indent
1496$ echo -e "a\evb"
1497a
1498 b
1499$ echo -e a\e\evb
1500a
1501 b
1502$ echo -e "a\e\eb"
1503a\eb
1504$ echo -e a\e\e\e\eb
1505a\eb
1506.Ed
1507.El
1508.Pp
1509Only one of the
1510.Fl e
1511and
1512.Fl n
1513options may be specified.
1514.It Ic eval Ar string ...
1515Concatenate all the arguments with spaces.
1516Then re-parse and execute the command.
1517.It Ic exec Op Ar command Op arg ...
1518Unless
1519.Ar command
1520is omitted,
1521the shell process is replaced with the specified program
1522(which must be a real program, not a shell built-in command or function).
1523Any redirections on the
1524.Ic exec
1525command are marked as permanent,
1526so that they are not undone when the
1527.Ic exec
1528command finishes.
1529.It Ic exit Op Ar exitstatus
1530Terminate the shell process.
1531If
1532.Ar exitstatus
1533is given
1534it is used as the exit status of the shell;
1535otherwise the exit status of the preceding command is used.
1536.It Ic export Oo Fl p Oc Op Ar name ...
1537The specified names are exported so that they will
1538appear in the environment of subsequent commands.
1539The only way to un-export a variable is to
1540.Ic unset
1541it.
1542The shell allows the value of a variable to be set
1543at the same time as it is exported by writing
1544.Bd -literal -offset indent
1545export name=value
1546.Ed
1547.Pp
1548With no arguments the export command lists the names
1549of all exported variables.
1550If the
1551.Fl p
1552option is specified, the exported variables are printed as
1553.Dq Ic export Ar name Ns = Ns Ar value
1554lines, suitable for re-input to the shell.
1555.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
1556.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
1557.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
1558The
1559.Ic fc
1560built-in command lists, or edits and re-executes,
1561commands previously entered to an interactive shell.
1562.Bl -tag -width indent
1563.It Fl e Ar editor
1564Use the editor named by
1565.Ar editor
1566to edit the commands.
1567The editor string is a command name,
1568subject to search via the
1569.Ev PATH
1570variable.
1571The value in the
1572.Ev FCEDIT
1573variable is used as a default when
1574.Fl e
1575is not specified.
1576If
1577.Ev FCEDIT
1578is null or unset, the value of the
1579.Ev EDITOR
1580variable is used.
1581If
1582.Ev EDITOR
1583is null or unset,
1584.Xr ed 1
1585is used as the editor.
1586.It Fl l No (ell)
1587List the commands rather than invoking
1588an editor on them.
1589The commands are written in the
1590sequence indicated by the first and last operands, as
1591affected by
1592.Fl r ,
1593with each command preceded by the command number.
1594.It Fl n
1595Suppress command numbers when listing with
1596.Fl l .
1597.It Fl r
1598Reverse the order of the commands listed
1599(with
1600.Fl l )
1601or edited
1602(with neither
1603.Fl l
1604nor
1605.Fl s ) .
1606.It Fl s
1607Re-execute the command without invoking an editor.
1608.It Ar first
1609.It Ar last
1610Select the commands to list or edit.
1611The number of previous commands that can be accessed
1612are determined by the value of the
1613.Ev HISTSIZE
1614variable.
1615The value of
1616.Ar first
1617or
1618.Ar last
1619or both are one of the following:
1620.Bl -tag -width indent
1621.It Ar [+]num
1622A positive number representing a command number;
1623command numbers can be displayed with the
1624.Fl l
1625option.
1626.It Ar -num
1627A negative decimal number representing the
1628command that was executed
1629.Ar num
1630of
1631commands previously.
1632For example, -1 is the immediately previous command.
1633.It Ar string
1634A string indicating the most recently entered command
1635that begins with that string.
1636If the
1637.Ar old=new
1638operand is not also specified with
1639.Fl s ,
1640the string form of the first operand cannot contain an embedded equal sign.
1641.El
1642.El
1643.Pp
1644The following environment variables affect the execution of
1645.Ic fc :
1646.Bl -tag -width indent
1647.It Ev FCEDIT
1648Name of the editor to use.
1649.It Ev HISTSIZE
1650The number of previous commands that are accessible.
1651.El
1652.It Ic fg Op Ar job
1653Move the specified
1654.Ar job
1655or the current job to the foreground.
1656.It Ic getopts Ar optstring Ar var
1657The POSIX
1658.Ic getopts
1659command.
1660The
1661.Ic getopts
1662command deprecates the older
1663.Xr getopt 1
1664command.
1665The first argument should be a series of letters, each possibly
1666followed by a colon which indicates that the option takes an argument.
1667The specified variable is set to the parsed option.
1668The index of
1669the next argument is placed into the shell variable
1670.Ev OPTIND .
1671If an option takes an argument, it is placed into the shell variable
1672.Ev OPTARG .
1673If an invalid option is encountered,
1674.Ev var
1675is set to
1676.Dq Li \&? .
1677It returns a false value (1) when it encounters the end of the options.
1678.It Ic hash Oo Fl rv Oc Op Ar command ...
1679The shell maintains a hash table which remembers the locations of commands.
1680With no arguments whatsoever, the
1681.Ic hash
1682command prints out the contents of this table.
1683Entries which have not been looked at since the last
1684.Ic cd
1685command are marked with an asterisk;
1686it is possible for these entries to be invalid.
1687.Pp
1688With arguments, the
1689.Ic hash
1690command removes each specified
1691.Ar command
1692from the hash table (unless they are functions) and then locates it.
1693With the
1694.Fl v
1695option,
1696.Ic hash
1697prints the locations of the commands as it finds them.
1698The
1699.Fl r
1700option causes the
1701.Ic hash
1702command to delete all the entries in the hash table except for functions.
1703.It Ic jobid Op Ar job
1704Print the process id's of the processes in the specified
1705.Ar job .
1706If the
1707.Ar job
1708argument is omitted, use the current job.
1709.It Ic jobs Oo Fl ls Oc Op Ar job ...
1710Print information about the specified jobs, or all jobs if no
1711.Ar job
1712argument is given.
1713The information printed includes job ID, status and command name.
1714.Pp
1715If the
1716.Fl l
1717option is specified, the PID of each job is also printed.
1718If the
1719.Fl s
1720option is specified, only the PIDs of the jobs are printed, one per line.
1721.It Ic pwd Op Fl L | P
1722Print the path of the current directory.
1723The built-in command may
1724differ from the program of the same name because the
1725built-in command remembers what the current directory
1726is rather than recomputing it each time.
1727This makes
1728it faster.
1729However, if the current directory is
1730renamed,
1731the built-in version of
1732.Xr pwd 1
1733will continue to print the old name for the directory.
1734.Pp
1735If the
1736.Fl P
1737option is specified, symbolic links are resolved.
1738If the
1739.Fl L
1740option is specified, the shell's notion of the current directory
1741is printed (symbolic links are not resolved).
1742This is the default.
1743.It Ic read Oo Fl p Ar prompt Oc Oo Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
1744The
1745.Ar prompt
1746is printed if the
1747.Fl p
1748option is specified
1749and the standard input is a terminal.
1750Then a line is
1751read from the standard input.
1752The trailing newline
1753is deleted from the line and the line is split as
1754described in the section on
1755.Sx White Space Splitting (Field Splitting)
1756above, and
1757the pieces are assigned to the variables in order.
1758If there are more pieces than variables, the remaining
1759pieces (along with the characters in
1760.Ev IFS
1761that separated them)
1762are assigned to the last variable.
1763If there are more variables than pieces, the remaining
1764variables are assigned the null string.
1765.Pp
1766Backslashes are treated specially, unless the
1767.Fl r
1768option is
1769specified.
1770If a backslash is followed by
1771a newline, the backslash and the newline will be
1772deleted.
1773If a backslash is followed by any other
1774character, the backslash will be deleted and the following
1775character will be treated as though it were not in
1776.Ev IFS ,
1777even if it is.
1778.Pp
1779If the
1780.Fl t
1781option is specified and the
1782.Ar timeout
1783elapses before any input is supplied,
1784the
1785.Ic read
1786command will return without assigning any values.
1787The
1788.Ar timeout
1789value may optionally be followed by one of
1790.Dq s ,
1791.Dq m
1792or
1793.Dq h
1794to explicitly specify seconds, minutes or hours.
1795If none is supplied,
1796.Dq s
1797is assumed.
1798.Pp
1799The
1800.Fl e
1801option exists only for backward compatibility with older scripts.
1802.It Ic readonly Oo Fl p Oc Op Ar name ...
1803Each specified
1804.Ar name
1805is marked as read only,
1806so that it cannot be subsequently modified or unset.
1807The shell allows the value of a variable to be set
1808at the same time as it is marked read only
1809by using the following form:
1810.Bd -literal -offset indent
1811readonly name=value
1812.Ed
1813.Pp
1814With no arguments the
1815.Ic readonly
1816command lists the names of all read only variables.
1817If the
1818.Fl p
1819option is specified, the read-only variables are printed as
1820.Dq Ic readonly Ar name Ns = Ns Ar value
1821lines, suitable for re-input to the shell.
1822.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
1823.Fl c Ar string Oc Op Fl - Ar arg ...
1824The
1825.Ic set
1826command performs three different functions:
1827.Bl -item
1828.It
1829With no arguments, it lists the values of all shell variables.
1830.It
1831If options are given,
1832either in short form or using the long
1833.Dq Fl /+o Ar longname
1834form,
1835it sets or clears the specified options as described in the section called
1836.Sx Argument List Processing .
1837.It
1838If the
1839.Dq Fl -
1840option is specified,
1841.Ic set
1842will replace the shell's positional parameters with the subsequent
1843arguments.
1844If no arguments follow the
1845.Dq Fl -
1846option,
1847all the positional parameters will be cleared,
1848which is equivalent to executing the command
1849.Dq Li shift $# .
1850The
1851.Dq Fl -
1852flag may be omitted when specifying arguments to be used
1853as positional replacement parameters.
1854This is not recommended,
1855because the first argument may begin with a dash
1856.Pq Li -
1857or a plus
1858.Pq Li + ,
1859which the
1860.Ic set
1861command will interpret as a request to enable or disable options.
1862.El
1863.It Ic setvar Ar variable Ar value
1864Assigns the specified
1865.Ar value
1866to the specified
1867.Ar variable .
1868.Ic Setvar
1869is intended to be used in functions that
1870assign values to variables whose names are passed as parameters.
1871In general it is better to write
1872.Bd -literal -offset indent
1873variable=value
1874.Ed
1875rather than using
1876.Ic setvar .
1877.It Ic shift Op Ar n
1878Shift the positional parameters
1879.Ar n
1880times, or once if
1881.Ar n
1882is not specified.
1883A shift sets the value of $1 to the value of $2,
1884the value of $2 to the value of $3, and so on,
1885decreasing the value of $# by one.
1886If there are zero positional parameters, shifting does not do anything.
1887.It Ic trap Oo Ar action Oc Ar signal ...
1888Cause the shell to parse and execute
1889.Ar action
1890when any specified
1891.Ar signal
1892is received.
1893The signals are specified by name or number.
1894In addition, the pseudo-signal
1895.Cm EXIT
1896may be used to specify an action that is performed when the shell terminates.
1897The
1898.Ar action
1899may be null or omitted;
1900the former causes the specified signal to be ignored
1901and the latter causes the default action to be taken.
1902When the shell forks off a subshell,
1903it resets trapped (but not ignored) signals to the default action.
1904The
1905.Ic trap
1906command has no effect on signals that were ignored on entry to the shell.
1907.It Ic type Op Ar name ...
1908Interpret each
1909.Ar name
1910as a command and print the resolution of the command search.
1911Possible resolutions are:
1912shell keyword, alias, shell built-in command, command, tracked alias
1913and not found.
1914For aliases the alias expansion is printed;
1915for commands and tracked aliases
1916the complete pathname of the command is printed.
1917.It Ic ulimit Oo Fl HSabcdflmnstuv Oc Op Ar limit
1918Set or display resource limits (see
1919.Xr getrlimit 2 ) .
1920If
1921.Ar limit
1922is specified, the named resource will be set;
1923otherwise the current resource value will be displayed.
1924.Pp
1925If
1926.Fl H
1927is specified, the hard limits will be set or displayed.
1928While everybody is allowed to reduce a hard limit,
1929only the superuser can increase it.
1930The
1931.Fl S
1932option
1933specifies the soft limits instead.
1934When displaying limits,
1935only one of
1936.Fl S
1937or
1938.Fl H
1939can be given.
1940The default is to display the soft limits,
1941and to set both the hard and the soft limits.
1942.Pp
1943Option
1944.Fl a
1945causes the
1946.Ic ulimit
1947command to display all resources.
1948The parameter
1949.Ar limit
1950is not acceptable in this mode.
1951.Pp
1952The remaining options specify which resource value is to be
1953displayed or modified.
1954They are mutually exclusive.
1955.Bl -tag -width indent
1956.It Fl b Ar sbsize
1957The maximum size of socket buffer usage, in bytes.
1958.It Fl c Ar coredumpsize
1959The maximal size of core dump files, in 512-byte blocks.
1960.It Fl d Ar datasize
1961The maximal size of the data segment of a process, in kilobytes.
1962.It Fl f Ar filesize
1963The maximal size of a file, in 512-byte blocks.
1964.It Fl l Ar lockedmem
1965The maximal size of memory that can be locked by a process, in
1966kilobytes.
1967.It Fl m Ar memoryuse
1968The maximal resident set size of a process, in kilobytes.
1969.It Fl n Ar nofiles
1970The maximal number of descriptors that could be opened by a process.
1971.It Fl s Ar stacksize
1972The maximal size of the stack segment, in kilobytes.
1973.It Fl t Ar time
1974The maximal amount of CPU time to be used by each process, in seconds.
1975.It Fl u Ar userproc
1976The maximal number of simultaneous processes for this user ID.
1977.It Fl v Ar virtualmem
1978The maximal virtual size of a process, in kilobytes.
1979.El
1980.It Ic umask Op Ar mask
1981Set the file creation mask (see
1982.Xr umask 2 )
1983to the octal value specified by
1984.Ar mask .
1985If the argument is omitted, the current mask value is printed.
1986.It Ic unalias Oo Fl a Oc Op Ar name
1987If
1988.Ar name
1989is specified, the shell removes that alias.
1990If
1991.Fl a
1992is specified, all aliases are removed.
1993.It Ic unset Oo Fl fv Oc Ar name ...
1994The specified variables or functions are unset and unexported.
1995If the
1996.Fl v
1997option is specified or no options are given, the
1998.Ar name
1999arguments are treated as variable names.
2000If the
2001.Fl f
2002option is specified, the
2003.Ar name
2004arguments are treated as function names.
2005.It Ic wait Op Ar job
2006Wait for the specified
2007.Ar job
2008to complete and return the exit status of the last process in the
2009.Ar job .
2010If the argument is omitted, wait for all jobs to complete
2011and return an exit status of zero.
2012.El
2013.Ss Commandline Editing
2014When
2015.Nm
2016is being used interactively from a terminal, the current command
2017and the command history
2018(see
2019.Ic fc
2020in
2021.Sx Built-in Commands )
2022can be edited using vi-mode command line editing.
2023This mode uses commands similar
2024to a subset of those described in the vi man page.
2025The command
2026.Dq Li set -o vi
2027(or
2028.Dq Li set -V )
2029enables vi-mode editing and places
2030.Nm
2031into vi insert mode.
2032With vi-mode enabled,
2033.Nm
2034can be switched between insert mode and command mode by typing
2035.Aq ESC .
2036Hitting
2037.Aq return
2038while in command mode will pass the line to the shell.
2039.Pp
2040Similarly, the
2041.Dq Li set -o emacs
2042(or
2043.Dq Li set -E )
2044command can be used to enable a subset of
2045emacs-style command line editing features.
2046.Sh SEE ALSO
2047.Xr builtin 1 ,
2048.Xr echo 1 ,
2049.Xr expr 1 ,
2050.Xr pwd 1 ,
2051.Xr test 1
2052.Sh HISTORY
2053A
2054.Nm
2055command appeared in
2056.At v1 .
2057.Sh BUGS
2058The
2059.Nm
2060utility does not recognize multibyte characters.
2061