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