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