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