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