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