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