xref: /freebsd/bin/sh/sh.1 (revision aa0a1e58)
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 March 20, 2011
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 )
650but appears to be a text file,
651the shell will run a new instance of
652.Nm
653to interpret it.
654.Pp
655Note that previous versions of this document
656and the source code itself misleadingly and sporadically
657refer to a shell script without a magic number
658as a
659.Dq "shell procedure" .
660.Ss Path Search
661When locating a command, the shell first looks to see if
662it has a shell function by that name.
663Then it looks for a
664built-in command by that name.
665If a built-in command is not found,
666one of two things happen:
667.Bl -enum
668.It
669Command names containing a slash are simply executed without
670performing any searches.
671.It
672The shell searches each entry in the
673.Va PATH
674variable
675in turn for the command.
676The value of the
677.Va PATH
678variable should be a series of
679entries separated by colons.
680Each entry consists of a
681directory name.
682The current directory
683may be indicated implicitly by an empty directory name,
684or explicitly by a single period.
685.El
686.Ss Command Exit Status
687Each command has an exit status that can influence the behavior
688of other shell commands.
689The paradigm is that a command exits
690with zero for normal or success, and non-zero for failure,
691error, or a false indication.
692The man page for each command
693should indicate the various exit codes and what they mean.
694Additionally, the built-in commands return exit codes, as does
695an executed shell function.
696.Pp
697If a command is terminated by a signal, its exit status is 128 plus
698the signal number.
699Signal numbers are defined in the header file
700.In sys/signal.h .
701.Ss Complex Commands
702Complex commands are combinations of simple commands
703with control operators or keywords, together creating a larger complex
704command.
705More generally, a command is one of the following:
706.Bl -item -offset indent
707.It
708simple command
709.It
710pipeline
711.It
712list or compound-list
713.It
714compound command
715.It
716function definition
717.El
718.Pp
719Unless otherwise stated, the exit status of a command is
720that of the last simple command executed by the command.
721.Ss Pipelines
722A pipeline is a sequence of one or more commands separated
723by the control operator
724.Ql \&| .
725The standard output of all but
726the last command is connected to the standard input
727of the next command.
728The standard output of the last
729command is inherited from the shell, as usual.
730.Pp
731The format for a pipeline is:
732.Pp
733.D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ...
734.Pp
735The standard output of
736.Ar command1
737is connected to the standard input of
738.Ar command2 .
739The standard input, standard output, or
740both of a command is considered to be assigned by the
741pipeline before any redirection specified by redirection
742operators that are part of the command.
743.Pp
744Note that unlike some other shells,
745.Nm
746executes each process in a pipeline with more than one command
747in a subshell environment and as a child of the
748.Nm
749process.
750.Pp
751If the pipeline is not in the background (discussed later),
752the shell waits for all commands to complete.
753.Pp
754If the keyword
755.Ic !\&
756does not precede the pipeline, the
757exit status is the exit status of the last command specified
758in the pipeline.
759Otherwise, the exit status is the logical
760NOT of the exit status of the last command.
761That is, if
762the last command returns zero, the exit status is 1; if
763the last command returns greater than zero, the exit status
764is zero.
765.Pp
766Because pipeline assignment of standard input or standard
767output or both takes place before redirection, it can be
768modified by redirection.
769For example:
770.Pp
771.Dl "command1 2>&1 | command2"
772.Pp
773sends both the standard output and standard error of
774.Ar command1
775to the standard input of
776.Ar command2 .
777.Pp
778A
779.Ql \&;
780or newline terminator causes the preceding
781AND-OR-list
782(described below in the section called
783.Sx Short-Circuit List Operators )
784to be executed sequentially;
785an
786.Ql &
787causes asynchronous execution of the preceding AND-OR-list.
788.Ss Background Commands (&)
789If a command is terminated by the control operator ampersand
790.Pq Ql & ,
791the shell executes the command asynchronously;
792the shell does not wait for the command to finish
793before executing the next command.
794.Pp
795The format for running a command in background is:
796.Pp
797.D1 Ar command1 Li & Op Ar command2 Li & Ar ...
798.Pp
799If the shell is not interactive, the standard input of an
800asynchronous command is set to
801.Pa /dev/null .
802.Ss Lists (Generally Speaking)
803A list is a sequence of zero or more commands separated by
804newlines, semicolons, or ampersands,
805and optionally terminated by one of these three characters.
806The commands in a
807list are executed in the order they are written.
808If command is followed by an ampersand, the shell starts the
809command and immediately proceeds onto the next command;
810otherwise it waits for the command to terminate before
811proceeding to the next one.
812.Ss Short-Circuit List Operators
813.Dq Li &&
814and
815.Dq Li ||
816are AND-OR list operators.
817.Dq Li &&
818executes the first command, and then executes the second command
819if the exit status of the first command is zero.
820.Dq Li ||
821is similar, but executes the second command if the exit
822status of the first command is nonzero.
823.Dq Li &&
824and
825.Dq Li ||
826both have the same priority.
827.Ss Flow-Control Constructs (if, while, for, case)
828The syntax of the
829.Ic if
830command is:
831.Bd -unfilled -offset indent -compact
832.Ic if Ar list
833.Ic then Ar list
834.Oo Ic elif Ar list
835.Ic then Ar list Oc Ar ...
836.Op Ic else Ar list
837.Ic fi
838.Ed
839.Pp
840The syntax of the
841.Ic while
842command is:
843.Bd -unfilled -offset indent -compact
844.Ic while Ar list
845.Ic do Ar list
846.Ic done
847.Ed
848.Pp
849The two lists are executed repeatedly while the exit status of the
850first list is zero.
851The
852.Ic until
853command is similar, but has the word
854.Ic until
855in place of
856.Ic while ,
857which causes it to
858repeat until the exit status of the first list is zero.
859.Pp
860The syntax of the
861.Ic for
862command is:
863.Bd -unfilled -offset indent -compact
864.Ic for Ar variable Op Ic in Ar word ...
865.Ic do Ar list
866.Ic done
867.Ed
868.Pp
869If
870.Ic in
871and the following words are omitted,
872.Ic in Li \&"$@\&"
873is used instead.
874The words are expanded, and then the list is executed
875repeatedly with the variable set to each word in turn.
876The
877.Ic do
878and
879.Ic done
880commands may be replaced with
881.Ql {
882and
883.Ql } .
884.Pp
885The syntax of the
886.Ic break
887and
888.Ic continue
889commands is:
890.D1 Ic break Op Ar num
891.D1 Ic continue Op Ar num
892.Pp
893The
894.Ic break
895command terminates the
896.Ar num
897innermost
898.Ic for
899or
900.Ic while
901loops.
902The
903.Ic continue
904command continues with the next iteration of the innermost loop.
905These are implemented as special built-in commands.
906.Pp
907The syntax of the
908.Ic case
909command is:
910.Bd -unfilled -offset indent -compact
911.Ic case Ar word Ic in
912.Ar pattern Ns Li ) Ar list Li ;;
913.Ar ...
914.Ic esac
915.Ed
916.Pp
917The pattern can actually be one or more patterns
918(see
919.Sx Shell Patterns
920described later),
921separated by
922.Ql \&|
923characters.
924The exit code of the
925.Ic case
926command is the exit code of the last command executed in the list or
927zero if no patterns were matched.
928.Ss Grouping Commands Together
929Commands may be grouped by writing either
930.Pp
931.D1 Li \&( Ns Ar list Ns Li \%)
932.Pp
933or
934.Pp
935.D1 Li { Ar list Ns Li \&; }
936.Pp
937The first form executes the commands in a subshell environment.
938Note that built-in commands thus executed do not affect the current shell.
939The second form never forks another shell,
940so it is slightly more efficient.
941Grouping commands together this way allows the user to
942redirect their output as though they were one program:
943.Bd -literal -offset indent
944{ echo -n "hello"; echo " world"; } > greeting
945.Ed
946.Ss Functions
947The syntax of a function definition is
948.Pp
949.D1 Ar name Li \&( \&) Ar command
950.Pp
951A function definition is an executable statement; when
952executed it installs a function named
953.Ar name
954and returns an
955exit status of zero.
956The
957.Ar command
958is normally a list
959enclosed between
960.Ql {
961and
962.Ql } .
963.Pp
964Variables may be declared to be local to a function by
965using the
966.Ic local
967command.
968This should appear as the first statement of a function,
969and the syntax is:
970.Pp
971.D1 Ic local Oo Ar variable ... Oc Op Fl
972.Pp
973The
974.Ic local
975command is implemented as a built-in command.
976.Pp
977When a variable is made local, it inherits the initial
978value and exported and readonly flags from the variable
979with the same name in the surrounding scope, if there is
980one.
981Otherwise, the variable is initially unset.
982The shell
983uses dynamic scoping, so that if the variable
984.Va x
985is made local to function
986.Em f ,
987which then calls function
988.Em g ,
989references to the variable
990.Va x
991made inside
992.Em g
993will refer to the variable
994.Va x
995declared inside
996.Em f ,
997not to the global variable named
998.Va x .
999.Pp
1000The only special parameter that can be made local is
1001.Ql - .
1002Making
1003.Ql -
1004local causes any shell options that are
1005changed via the
1006.Ic set
1007command inside the function to be
1008restored to their original values when the function
1009returns.
1010.Pp
1011The syntax of the
1012.Ic return
1013command is
1014.Pp
1015.D1 Ic return Op Ar exitstatus
1016.Pp
1017It terminates the current executional scope, returning from the previous
1018nested function, sourced script, or shell instance, in that order.
1019The
1020.Ic return
1021command is implemented as a special built-in command.
1022.Ss Variables and Parameters
1023The shell maintains a set of parameters.
1024A parameter
1025denoted by a name is called a variable.
1026When starting up,
1027the shell turns all the environment variables into shell
1028variables.
1029New variables can be set using the form
1030.Pp
1031.D1 Ar name Ns = Ns Ar value
1032.Pp
1033Variables set by the user must have a name consisting solely
1034of alphabetics, numerics, and underscores.
1035The first letter of a variable name must not be numeric.
1036A parameter can also be denoted by a number
1037or a special character as explained below.
1038.Ss Positional Parameters
1039A positional parameter is a parameter denoted by a number greater than zero.
1040The shell sets these initially to the values of its command line
1041arguments that follow the name of the shell script.
1042The
1043.Ic set
1044built-in command can also be used to set or reset them.
1045.Ss Special Parameters
1046Special parameters are parameters denoted by a single special character
1047or the digit zero.
1048They are shown in the following list, exactly as they would appear in input
1049typed by the user or in the source of a shell script.
1050.Bl -hang
1051.It Li $*
1052Expands to the positional parameters, starting from one.
1053When
1054the expansion occurs within a double-quoted string
1055it expands to a single field with the value of each parameter
1056separated by the first character of the
1057.Va IFS
1058variable,
1059or by a space if
1060.Va IFS
1061is unset.
1062.It Li $@
1063Expands to the positional parameters, starting from one.
1064When
1065the expansion occurs within double-quotes, each positional
1066parameter expands as a separate argument.
1067If there are no positional parameters, the
1068expansion of
1069.Li @
1070generates zero arguments, even when
1071.Li @
1072is double-quoted.
1073What this basically means, for example, is
1074if
1075.Li $1
1076is
1077.Dq Li abc
1078and
1079.Li $2
1080is
1081.Dq Li "def ghi" ,
1082then
1083.Li \&"$@\&"
1084expands to
1085the two arguments:
1086.Bd -literal -offset indent
1087"abc"   "def ghi"
1088.Ed
1089.It Li $#
1090Expands to the number of positional parameters.
1091.It Li $?
1092Expands to the exit status of the most recent pipeline.
1093.It Li $-
1094(hyphen) Expands to the current option flags (the single-letter
1095option names concatenated into a string) as specified on
1096invocation, by the
1097.Ic set
1098built-in command, or implicitly
1099by the shell.
1100.It Li $$
1101Expands to the process ID of the invoked shell.
1102A subshell
1103retains the same value of
1104.Va $
1105as its parent.
1106.It Li $!
1107Expands to the process ID of the most recent background
1108command executed from the current shell.
1109For a
1110pipeline, the process ID is that of the last command in the
1111pipeline.
1112If this parameter is referenced, the shell will remember
1113the process ID and its exit status until the
1114.Ic wait
1115built-in command reports completion of the process.
1116.It Li $0
1117(zero) Expands to the name of the shell script if passed on the command line,
1118the
1119.Ar name
1120operand if given (with
1121.Fl c )
1122or otherwise argument 0 passed to the shell.
1123.El
1124.Ss Special Variables
1125The following variables are set by the shell or
1126have special meaning to it:
1127.Bl -tag -width ".Va HISTSIZE"
1128.It Va CDPATH
1129The search path used with the
1130.Ic cd
1131built-in.
1132.It Va EDITOR
1133The fallback editor used with the
1134.Ic fc
1135built-in.
1136If not set, the default editor is
1137.Xr ed 1 .
1138.It Va FCEDIT
1139The default editor used with the
1140.Ic fc
1141built-in.
1142.It Va HISTSIZE
1143The number of previous commands that are accessible.
1144.It Va HOME
1145The user's home directory,
1146used in tilde expansion and as a default directory for the
1147.Ic cd
1148built-in.
1149.It Va IFS
1150Input Field Separators.
1151This is normally set to
1152.Aq space ,
1153.Aq tab ,
1154and
1155.Aq newline .
1156See the
1157.Sx White Space Splitting
1158section for more details.
1159.It Va LINENO
1160The current line number in the script or function.
1161.It Va MAIL
1162The name of a mail file, that will be checked for the arrival of new
1163mail.
1164Overridden by
1165.Va MAILPATH .
1166.It Va MAILPATH
1167A colon
1168.Pq Ql \&:
1169separated list of file names, for the shell to check for incoming
1170mail.
1171This variable overrides the
1172.Va MAIL
1173setting.
1174There is a maximum of 10 mailboxes that can be monitored at once.
1175.It Va PATH
1176The default search path for executables.
1177See the
1178.Sx Path Search
1179section for details.
1180.It Va PPID
1181The parent process ID of the invoked shell.
1182This is set at startup
1183unless this variable is in the environment.
1184A later change of parent process ID is not reflected.
1185A subshell retains the same value of
1186.Va PPID .
1187.It Va PS1
1188The primary prompt string, which defaults to
1189.Dq Li "$ " ,
1190unless you are the superuser, in which case it defaults to
1191.Dq Li "# " .
1192.It Va PS2
1193The secondary prompt string, which defaults to
1194.Dq Li "> " .
1195.It Va PS4
1196The prefix for the trace output (if
1197.Fl x
1198is active).
1199The default is
1200.Dq Li "+ " .
1201.El
1202.Ss Word Expansions
1203This clause describes the various expansions that are
1204performed on words.
1205Not all expansions are performed on
1206every word, as explained later.
1207.Pp
1208Tilde expansions, parameter expansions, command substitutions,
1209arithmetic expansions, and quote removals that occur within
1210a single word expand to a single field.
1211It is only field
1212splitting or pathname expansion that can create multiple
1213fields from a single word.
1214The single exception to this rule is
1215the expansion of the special parameter
1216.Va @
1217within double-quotes,
1218as was described above.
1219.Pp
1220The order of word expansion is:
1221.Bl -enum
1222.It
1223Tilde Expansion, Parameter Expansion, Command Substitution,
1224Arithmetic Expansion (these all occur at the same time).
1225.It
1226Field Splitting is performed on fields generated by step (1)
1227unless the
1228.Va IFS
1229variable is null.
1230.It
1231Pathname Expansion (unless the
1232.Fl f
1233option is in effect).
1234.It
1235Quote Removal.
1236.El
1237.Pp
1238The
1239.Ql $
1240character is used to introduce parameter expansion, command
1241substitution, or arithmetic expansion.
1242.Ss Tilde Expansion (substituting a user's home directory)
1243A word beginning with an unquoted tilde character
1244.Pq Ql ~
1245is
1246subjected to tilde expansion.
1247All the characters up to a slash
1248.Pq Ql /
1249or the end of the word are treated as a username
1250and are replaced with the user's home directory.
1251If the
1252username is missing (as in
1253.Pa ~/foobar ) ,
1254the tilde is replaced with the value of the
1255.Va HOME
1256variable (the current user's home directory).
1257.Ss Parameter Expansion
1258The format for parameter expansion is as follows:
1259.Pp
1260.D1 Li ${ Ns Ar expression Ns Li }
1261.Pp
1262where
1263.Ar expression
1264consists of all characters until the matching
1265.Ql } .
1266Any
1267.Ql }
1268escaped by a backslash or within a single-quoted or double-quoted
1269string, and characters in
1270embedded arithmetic expansions, command substitutions, and variable
1271expansions, are not examined in determining the matching
1272.Ql } .
1273If the variants with
1274.Ql + ,
1275.Ql - ,
1276.Ql =
1277or
1278.Ql ?\&
1279occur within a double-quoted string,
1280as an extension there may be unquoted parts
1281(via double-quotes inside the expansion);
1282.Ql }
1283within such parts are also not examined in determining the matching
1284.Ql } .
1285.Pp
1286The simplest form for parameter expansion is:
1287.Pp
1288.D1 Li ${ Ns Ar parameter Ns Li }
1289.Pp
1290The value, if any, of
1291.Ar parameter
1292is substituted.
1293.Pp
1294The parameter name or symbol can be enclosed in braces, which are
1295optional except for positional parameters with more than one digit or
1296when parameter is followed by a character that could be interpreted as
1297part of the name.
1298If a parameter expansion occurs inside double-quotes:
1299.Bl -enum
1300.It
1301Pathname expansion is not performed on the results of the
1302expansion.
1303.It
1304Field splitting is not performed on the results of the
1305expansion, with the exception of the special parameter
1306.Va @ .
1307.El
1308.Pp
1309In addition, a parameter expansion can be modified by using one of the
1310following formats.
1311.Bl -tag -width indent
1312.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li }
1313Use Default Values.
1314If
1315.Ar parameter
1316is unset or null, the expansion of
1317.Ar word
1318is substituted; otherwise, the value of
1319.Ar parameter
1320is substituted.
1321.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li }
1322Assign Default Values.
1323If
1324.Ar parameter
1325is unset or null, the expansion of
1326.Ar word
1327is assigned to
1328.Ar parameter .
1329In all cases, the
1330final value of
1331.Ar parameter
1332is substituted.
1333Quoting inside
1334.Ar word
1335does not prevent field splitting or pathname expansion.
1336Only variables, not positional
1337parameters or special parameters, can be
1338assigned in this way.
1339.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li }
1340Indicate Error if Null or Unset.
1341If
1342.Ar parameter
1343is unset or null, the expansion of
1344.Ar word
1345(or a message indicating it is unset if
1346.Ar word
1347is omitted) is written to standard
1348error and the shell exits with a nonzero
1349exit status.
1350Otherwise, the value of
1351.Ar parameter
1352is substituted.
1353An
1354interactive shell need not exit.
1355.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li }
1356Use Alternate Value.
1357If
1358.Ar parameter
1359is unset or null, null is substituted;
1360otherwise, the expansion of
1361.Ar word
1362is substituted.
1363.El
1364.Pp
1365In the parameter expansions shown previously, use of the colon in the
1366format results in a test for a parameter that is unset or null; omission
1367of the colon results in a test for a parameter that is only unset.
1368.Pp
1369The
1370.Ar word
1371inherits the type of quoting
1372(unquoted, double-quoted or here-document)
1373from the surroundings,
1374with the exception that a backslash that quotes a closing brace is removed
1375during quote removal.
1376.Bl -tag -width indent
1377.It Li ${# Ns Ar parameter Ns Li }
1378String Length.
1379The length in characters of
1380the value of
1381.Ar parameter .
1382.El
1383.Pp
1384The following four varieties of parameter expansion provide for substring
1385processing.
1386In each case, pattern matching notation
1387(see
1388.Sx Shell Patterns ) ,
1389rather than regular expression notation,
1390is used to evaluate the patterns.
1391If parameter is one of the special parameters
1392.Va *
1393or
1394.Va @ ,
1395the result of the expansion is unspecified.
1396Enclosing the full parameter expansion string in double-quotes does not
1397cause the following four varieties of pattern characters to be quoted,
1398whereas quoting characters within the braces has this effect.
1399.Bl -tag -width indent
1400.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li }
1401Remove Smallest Suffix Pattern.
1402The
1403.Ar word
1404is expanded to produce a pattern.
1405The
1406parameter expansion then results in
1407.Ar parameter ,
1408with the smallest portion of the
1409suffix matched by the pattern deleted.
1410.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li }
1411Remove Largest Suffix Pattern.
1412The
1413.Ar word
1414is expanded to produce a pattern.
1415The
1416parameter expansion then results in
1417.Ar parameter ,
1418with the largest portion of the
1419suffix matched by the pattern deleted.
1420.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li }
1421Remove Smallest Prefix Pattern.
1422The
1423.Ar word
1424is expanded to produce a pattern.
1425The
1426parameter expansion then results in
1427.Ar parameter ,
1428with the smallest portion of the
1429prefix matched by the pattern deleted.
1430.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li }
1431Remove Largest Prefix Pattern.
1432The
1433.Ar word
1434is expanded to produce a pattern.
1435The
1436parameter expansion then results in
1437.Ar parameter ,
1438with the largest portion of the
1439prefix matched by the pattern deleted.
1440.El
1441.Ss Command Substitution
1442Command substitution allows the output of a command to be substituted in
1443place of the command name itself.
1444Command substitution occurs when
1445the command is enclosed as follows:
1446.Pp
1447.D1 Li $( Ns Ar command Ns Li )\&
1448.Pp
1449or the backquoted version:
1450.Pp
1451.D1 Li ` Ns Ar command Ns Li `
1452.Pp
1453The shell expands the command substitution by executing command
1454and replacing the command substitution
1455with the standard output of the command,
1456removing sequences of one or more newlines at the end of the substitution.
1457Embedded newlines before the end of the output are not removed;
1458however, during field splitting, they may be translated into spaces
1459depending on the value of
1460.Va IFS
1461and the quoting that is in effect.
1462The command is executed in a subshell environment,
1463except that the built-in commands
1464.Ic jobid ,
1465.Ic jobs ,
1466.Ic times
1467and
1468.Ic trap
1469return information about the main shell environment
1470if they are the only command in a command substitution
1471and the substitutions in the command cannot cause side effects
1472(such as from assigning values to variables or referencing
1473.Li $! ).
1474.Ss Arithmetic Expansion
1475Arithmetic expansion provides a mechanism for evaluating an arithmetic
1476expression and substituting its value.
1477The format for arithmetic expansion is as follows:
1478.Pp
1479.D1 Li $(( Ns Ar expression Ns Li ))
1480.Pp
1481The
1482.Ar expression
1483is treated as if it were in double-quotes, except
1484that a double-quote inside the expression is not treated specially.
1485The
1486shell expands all tokens in the
1487.Ar expression
1488for parameter expansion,
1489command substitution,
1490arithmetic expansion
1491and quote removal.
1492.Pp
1493The allowed expressions are a subset of C expressions,
1494summarized below.
1495.Bl -tag -width "Variables" -offset indent
1496.It Values
1497All values are of type
1498.Ft intmax_t .
1499.It Constants
1500Decimal, octal (starting with
1501.Li 0 )
1502and hexadecimal (starting with
1503.Li 0x )
1504integer constants.
1505.It Variables
1506Shell variables can be read and written
1507and contain integer constants.
1508.It Unary operators
1509.Li "! ~ + -"
1510.It Binary operators
1511.Li "* / % + - << >> < <= > >= == != & ^ | && ||"
1512.It Assignment operators
1513.Li "= += -= *= /= %= <<= >>= &= ^= |="
1514.It Conditional operator
1515.Li "? :"
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 built-in commands.
1600.Bl -tag -width indent
1601.It Ic \&:
1602A null command that returns a 0 (true) exit value.
1603.It Ic \&. Ar file
1604The commands in the specified file are read and executed by the shell.
1605The
1606.Ic return
1607command may be used to return to the
1608.Ic \&.
1609command's caller.
1610If
1611.Ar file
1612contains any
1613.Ql /
1614characters, it is used as is.
1615Otherwise, the shell searches the
1616.Va PATH
1617for the file.
1618If it is not found in the
1619.Va PATH ,
1620it is sought in the current working directory.
1621.It Ic \&[
1622A built-in equivalent of
1623.Xr test 1 .
1624.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc
1625If
1626.Ar name Ns = Ns Ar string
1627is specified, the shell defines the alias
1628.Ar name
1629with value
1630.Ar string .
1631If just
1632.Ar name
1633is specified, the value of the alias
1634.Ar name
1635is printed.
1636With no arguments, the
1637.Ic alias
1638built-in command prints the names and values of all defined aliases
1639(see
1640.Ic unalias ) .
1641Alias values are written with appropriate quoting so that they are
1642suitable for re-input to the shell.
1643Also see the
1644.Sx Aliases
1645subsection.
1646.It Ic bg Op Ar job ...
1647Continue the specified jobs
1648(or the current job if no jobs are given)
1649in the background.
1650.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
1651List or alter key bindings for the line editor.
1652This command is documented in
1653.Xr editrc 5 .
1654.It Ic break Op Ar num
1655See the
1656.Sx Flow-Control Constructs
1657subsection.
1658.It Ic builtin Ar cmd Op Ar arg ...
1659Execute the specified built-in command,
1660.Ar cmd .
1661This is useful when the user wishes to override a shell function
1662with the same name as a built-in command.
1663.It Ic cd Oo Fl L | P Oc Op Ar directory
1664Switch to the specified
1665.Ar directory ,
1666or to the directory specified in the
1667.Va HOME
1668environment variable if no
1669.Ar directory
1670is specified.
1671If
1672.Ar directory
1673does not begin with
1674.Pa / , \&. ,
1675or
1676.Pa .. ,
1677then the directories listed in the
1678.Va CDPATH
1679variable will be
1680searched for the specified
1681.Ar directory .
1682If
1683.Va CDPATH
1684is unset, the current directory is searched.
1685The format of
1686.Va CDPATH
1687is the same as that of
1688.Va PATH .
1689In an interactive shell,
1690the
1691.Ic cd
1692command will print out the name of the directory
1693that it actually switched to
1694if this is different from the name that the user gave.
1695These may be different either because the
1696.Va CDPATH
1697mechanism was used or because a symbolic link was crossed.
1698.Pp
1699If the
1700.Fl P
1701option is specified,
1702.Pa ..
1703is handled physically and symbolic links are resolved before
1704.Pa ..
1705components are processed.
1706If the
1707.Fl L
1708option is specified,
1709.Pa ..
1710is handled logically.
1711This is the default.
1712.It Ic chdir
1713A synonym for the
1714.Ic cd
1715built-in command.
1716.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
1717.It Ic command Oo Fl p Oc Fl v Ar utility
1718.It Ic command Oo Fl p Oc Fl V 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 continue Op Ar num
1765See the
1766.Sx Flow-Control Constructs
1767subsection.
1768.It Ic echo Oo Fl e | n Oc Op Ar string ...
1769Print a space-separated list of the arguments to the standard output
1770and append a newline character.
1771.Bl -tag -width indent
1772.It Fl n
1773Suppress the output of the trailing newline.
1774.It Fl e
1775Process C-style backslash escape sequences.
1776The
1777.Ic echo
1778command understands the following character escapes:
1779.Bl -tag -width indent
1780.It \ea
1781Alert (ring the terminal bell)
1782.It \eb
1783Backspace
1784.It \ec
1785Suppress the trailing newline (this has the side-effect of truncating the
1786line if it is not the last character)
1787.It \ee
1788The ESC character
1789.Tn ( ASCII
17900x1b)
1791.It \ef
1792Formfeed
1793.It \en
1794Newline
1795.It \er
1796Carriage return
1797.It \et
1798Horizontal tab
1799.It \ev
1800Vertical tab
1801.It \e\e
1802Literal backslash
1803.It \e0nnn
1804(Zero) The character whose octal value is
1805.Ar nnn
1806.El
1807.Pp
1808If
1809.Ar string
1810is not enclosed in quotes then the backslash itself must be escaped
1811with a backslash to protect it from the shell.
1812For example
1813.Bd -literal -offset indent
1814$ echo -e "a\evb"
1815a
1816 b
1817$ echo -e a\e\evb
1818a
1819 b
1820$ echo -e "a\e\eb"
1821a\eb
1822$ echo -e a\e\e\e\eb
1823a\eb
1824.Ed
1825.El
1826.Pp
1827Only one of the
1828.Fl e
1829and
1830.Fl n
1831options may be specified.
1832.It Ic eval Ar string ...
1833Concatenate all the arguments with spaces.
1834Then re-parse and execute the command.
1835.It Ic exec Op Ar command Op arg ...
1836Unless
1837.Ar command
1838is omitted,
1839the shell process is replaced with the specified program
1840(which must be a real program, not a shell built-in command or function).
1841Any redirections on the
1842.Ic exec
1843command are marked as permanent,
1844so that they are not undone when the
1845.Ic exec
1846command finishes.
1847.It Ic exit Op Ar exitstatus
1848Terminate the shell process.
1849If
1850.Ar exitstatus
1851is given
1852it is used as the exit status of the shell.
1853Otherwise, if the shell is executing an
1854.Cm EXIT
1855trap, the exit status of the last command before the trap is used;
1856if the shell is executing a trap for a signal,
1857the shell exits by resending the signal to itself.
1858Otherwise, the exit status of the preceding command is used.
1859The exit status should be an integer between 0 and 255.
1860.It Ic export Ar name ...
1861.It Ic export Op Fl p
1862The specified names are exported so that they will
1863appear in the environment of subsequent commands.
1864The only way to un-export a variable is to
1865.Ic unset
1866it.
1867The shell allows the value of a variable to be set
1868at the same time as it is exported by writing
1869.Pp
1870.D1 Ic export Ar name Ns = Ns Ar value
1871.Pp
1872With no arguments the
1873.Ic export
1874command lists the names
1875of all exported variables.
1876If the
1877.Fl p
1878option is specified, the exported variables are printed as
1879.Dq Ic export Ar name Ns = Ns Ar value
1880lines, suitable for re-input to the shell.
1881.It Ic false
1882A null command that returns a non-zero (false) exit value.
1883.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
1884.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
1885.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
1886The
1887.Ic fc
1888built-in command lists, or edits and re-executes,
1889commands previously entered to an interactive shell.
1890.Bl -tag -width indent
1891.It Fl e Ar editor
1892Use the editor named by
1893.Ar editor
1894to edit the commands.
1895The
1896.Ar editor
1897string is a command name,
1898subject to search via the
1899.Va PATH
1900variable.
1901The value in the
1902.Va FCEDIT
1903variable is used as a default when
1904.Fl e
1905is not specified.
1906If
1907.Va FCEDIT
1908is null or unset, the value of the
1909.Va EDITOR
1910variable is used.
1911If
1912.Va EDITOR
1913is null or unset,
1914.Xr ed 1
1915is used as the editor.
1916.It Fl l No (ell)
1917List the commands rather than invoking
1918an editor on them.
1919The commands are written in the
1920sequence indicated by the
1921.Ar first
1922and
1923.Ar last
1924operands, as affected by
1925.Fl r ,
1926with each command preceded by the command number.
1927.It Fl n
1928Suppress command numbers when listing with
1929.Fl l .
1930.It Fl r
1931Reverse the order of the commands listed
1932(with
1933.Fl l )
1934or edited
1935(with neither
1936.Fl l
1937nor
1938.Fl s ) .
1939.It Fl s
1940Re-execute the command without invoking an editor.
1941.It Ar first
1942.It Ar last
1943Select the commands to list or edit.
1944The number of previous commands that can be accessed
1945are determined by the value of the
1946.Va HISTSIZE
1947variable.
1948The value of
1949.Ar first
1950or
1951.Ar last
1952or both are one of the following:
1953.Bl -tag -width indent
1954.It Oo Cm + Oc Ns Ar num
1955A positive number representing a command number;
1956command numbers can be displayed with the
1957.Fl l
1958option.
1959.It Fl Ar num
1960A negative decimal number representing the
1961command that was executed
1962.Ar num
1963of
1964commands previously.
1965For example, \-1 is the immediately previous command.
1966.It Ar string
1967A string indicating the most recently entered command
1968that begins with that string.
1969If the
1970.Ar old Ns = Ns Ar new
1971operand is not also specified with
1972.Fl s ,
1973the string form of the first operand cannot contain an embedded equal sign.
1974.El
1975.El
1976.Pp
1977The following variables affect the execution of
1978.Ic fc :
1979.Bl -tag -width ".Va HISTSIZE"
1980.It Va FCEDIT
1981Name of the editor to use for history editing.
1982.It Va HISTSIZE
1983The number of previous commands that are accessible.
1984.El
1985.It Ic fg Op Ar job
1986Move the specified
1987.Ar job
1988or the current job to the foreground.
1989.It Ic getopts Ar optstring var
1990The
1991.Tn POSIX
1992.Ic getopts
1993command.
1994The
1995.Ic getopts
1996command deprecates the older
1997.Xr getopt 1
1998command.
1999The first argument should be a series of letters, each possibly
2000followed by a colon which indicates that the option takes an argument.
2001The specified variable is set to the parsed option.
2002The index of
2003the next argument is placed into the shell variable
2004.Va OPTIND .
2005If an option takes an argument, it is placed into the shell variable
2006.Va OPTARG .
2007If an invalid option is encountered,
2008.Ar var
2009is set to
2010.Ql \&? .
2011It returns a false value (1) when it encounters the end of the options.
2012.It Ic hash Oo Fl rv Oc Op Ar command ...
2013The shell maintains a hash table which remembers the locations of commands.
2014With no arguments whatsoever, the
2015.Ic hash
2016command prints out the contents of this table.
2017Entries which have not been looked at since the last
2018.Ic cd
2019command are marked with an asterisk;
2020it is possible for these entries to be invalid.
2021.Pp
2022With arguments, the
2023.Ic hash
2024command removes each specified
2025.Ar command
2026from the hash table (unless they are functions) and then locates it.
2027With the
2028.Fl v
2029option,
2030.Ic hash
2031prints the locations of the commands as it finds them.
2032The
2033.Fl r
2034option causes the
2035.Ic hash
2036command to delete all the entries in the hash table except for functions.
2037.It Ic jobid Op Ar job
2038Print the process IDs of the processes in the specified
2039.Ar job .
2040If the
2041.Ar job
2042argument is omitted, use the current job.
2043.It Ic jobs Oo Fl lps Oc Op Ar job ...
2044Print information about the specified jobs, or all jobs if no
2045.Ar job
2046argument is given.
2047The information printed includes job ID, status and command name.
2048.Pp
2049If the
2050.Fl l
2051option is specified, the PID of each job is also printed.
2052If the
2053.Fl p
2054option is specified, only the process IDs for the process group leaders
2055are printed, one per line.
2056If the
2057.Fl s
2058option is specified, only the PIDs of the job commands are printed, one per
2059line.
2060.It Ic kill
2061A built-in equivalent of
2062.Xr kill 1
2063that additionally supports sending signals to jobs.
2064.It Ic local Oo Ar variable ... Oc Op Fl
2065See the
2066.Sx Functions
2067subsection.
2068.It Ic printf
2069A built-in equivalent of
2070.Xr printf 1 .
2071.It Ic pwd Op Fl L | P
2072Print the path of the current directory.
2073The built-in command may
2074differ from the program of the same name because the
2075built-in command remembers what the current directory
2076is rather than recomputing it each time.
2077This makes
2078it faster.
2079However, if the current directory is
2080renamed,
2081the built-in version of
2082.Xr pwd 1
2083will continue to print the old name for the directory.
2084.Pp
2085If the
2086.Fl P
2087option is specified, symbolic links are resolved.
2088If the
2089.Fl L
2090option is specified, the shell's notion of the current directory
2091is printed (symbolic links are not resolved).
2092This is the default.
2093.It Ic read Oo Fl p Ar prompt Oc Oo
2094.Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
2095The
2096.Ar prompt
2097is printed if the
2098.Fl p
2099option is specified
2100and the standard input is a terminal.
2101Then a line is
2102read from the standard input.
2103The trailing newline
2104is deleted from the line and the line is split as
2105described in the section on
2106.Sx White Space Splitting (Field Splitting)
2107above, and
2108the pieces are assigned to the variables in order.
2109If there are more pieces than variables, the remaining
2110pieces (along with the characters in
2111.Va IFS
2112that separated them)
2113are assigned to the last variable.
2114If there are more variables than pieces, the remaining
2115variables are assigned the null string.
2116.Pp
2117Backslashes are treated specially, unless the
2118.Fl r
2119option is
2120specified.
2121If a backslash is followed by
2122a newline, the backslash and the newline will be
2123deleted.
2124If a backslash is followed by any other
2125character, the backslash will be deleted and the following
2126character will be treated as though it were not in
2127.Va IFS ,
2128even if it is.
2129.Pp
2130If the
2131.Fl t
2132option is specified and the
2133.Ar timeout
2134elapses before a complete line of input is supplied,
2135the
2136.Ic read
2137command will return an exit status of 1 without assigning any values.
2138The
2139.Ar timeout
2140value may optionally be followed by one of
2141.Ql s ,
2142.Ql m
2143or
2144.Ql h
2145to explicitly specify seconds, minutes or hours.
2146If none is supplied,
2147.Ql s
2148is assumed.
2149.Pp
2150The
2151.Fl e
2152option exists only for backward compatibility with older scripts.
2153.It Ic readonly Oo Fl p Oc Op Ar name ...
2154Each specified
2155.Ar name
2156is marked as read only,
2157so that it cannot be subsequently modified or unset.
2158The shell allows the value of a variable to be set
2159at the same time as it is marked read only
2160by using the following form:
2161.Pp
2162.D1 Ic readonly Ar name Ns = Ns Ar value
2163.Pp
2164With no arguments the
2165.Ic readonly
2166command lists the names of all read only variables.
2167If the
2168.Fl p
2169option is specified, the read-only variables are printed as
2170.Dq Ic readonly Ar name Ns = Ns Ar value
2171lines, suitable for re-input to the shell.
2172.It Ic return Op Ar exitstatus
2173See the
2174.Sx Functions
2175subsection.
2176.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
2177.Fl c Ar string Oc Op Fl - Ar arg ...
2178The
2179.Ic set
2180command performs three different functions:
2181.Bl -item
2182.It
2183With no arguments, it lists the values of all shell variables.
2184.It
2185If options are given,
2186either in short form or using the long
2187.Dq Fl /+o Ar longname
2188form,
2189it sets or clears the specified options as described in the section called
2190.Sx Argument List Processing .
2191.It
2192If the
2193.Dq Fl -
2194option is specified,
2195.Ic set
2196will replace the shell's positional parameters with the subsequent
2197arguments.
2198If no arguments follow the
2199.Dq Fl -
2200option,
2201all the positional parameters will be cleared,
2202which is equivalent to executing the command
2203.Dq Li "shift $#" .
2204The
2205.Dq Fl -
2206flag may be omitted when specifying arguments to be used
2207as positional replacement parameters.
2208This is not recommended,
2209because the first argument may begin with a dash
2210.Pq Ql -
2211or a plus
2212.Pq Ql + ,
2213which the
2214.Ic set
2215command will interpret as a request to enable or disable options.
2216.El
2217.It Ic setvar Ar variable value
2218Assigns the specified
2219.Ar value
2220to the specified
2221.Ar variable .
2222The
2223.Ic setvar
2224command is intended to be used in functions that
2225assign values to variables whose names are passed as parameters.
2226In general it is better to write
2227.Dq Ar variable Ns = Ns Ar value
2228rather than using
2229.Ic setvar .
2230.It Ic shift Op Ar n
2231Shift the positional parameters
2232.Ar n
2233times, or once if
2234.Ar n
2235is not specified.
2236A shift sets the value of
2237.Li $1
2238to the value of
2239.Li $2 ,
2240the value of
2241.Li $2
2242to the value of
2243.Li $3 ,
2244and so on,
2245decreasing the value of
2246.Li $#
2247by one.
2248If there are zero positional parameters, shifting does not do anything.
2249.It Ic test
2250A built-in equivalent of
2251.Xr test 1 .
2252.It Ic times
2253Print the amount of time spent executing the shell and its children.
2254The first output line shows the user and system times for the shell
2255itself, the second one contains the user and system times for the
2256children.
2257.It Ic trap Oo Ar action Oc Ar signal ...
2258.It Ic trap Fl l
2259Cause the shell to parse and execute
2260.Ar action
2261when any specified
2262.Ar signal
2263is received.
2264The signals are specified by name or number.
2265In addition, the pseudo-signal
2266.Cm EXIT
2267may be used to specify an
2268.Ar action
2269that is performed when the shell terminates.
2270The
2271.Ar action
2272may be an empty string or a dash
2273.Pq Ql - ;
2274the former causes the specified signal to be ignored
2275and the latter causes the default action to be taken.
2276Omitting the
2277.Ar action
2278is another way to request the default action, for compatibility reasons this
2279usage is not recommended though.
2280In a subshell environment,
2281the shell resets trapped (but not ignored) signals to the default action.
2282The
2283.Ic trap
2284command has no effect on signals that were ignored on entry to the shell.
2285.Pp
2286Option
2287.Fl l
2288causes the
2289.Ic trap
2290command to display a list of valid signal names.
2291.It Ic true
2292A null command that returns a 0 (true) exit value.
2293.It Ic type Op Ar name ...
2294Interpret each
2295.Ar name
2296as a command and print the resolution of the command search.
2297Possible resolutions are:
2298shell keyword, alias, special shell builtin, shell builtin, command,
2299tracked alias
2300and not found.
2301For aliases the alias expansion is printed;
2302for commands and tracked aliases
2303the complete pathname of the command is printed.
2304.It Ic ulimit Oo Fl HSabcdflmnpstuvw Oc Op Ar limit
2305Set or display resource limits (see
2306.Xr getrlimit 2 ) .
2307If
2308.Ar limit
2309is specified, the named resource will be set;
2310otherwise the current resource value will be displayed.
2311.Pp
2312If
2313.Fl H
2314is specified, the hard limits will be set or displayed.
2315While everybody is allowed to reduce a hard limit,
2316only the superuser can increase it.
2317The
2318.Fl S
2319option
2320specifies the soft limits instead.
2321When displaying limits,
2322only one of
2323.Fl S
2324or
2325.Fl H
2326can be given.
2327The default is to display the soft limits,
2328and to set both the hard and the soft limits.
2329.Pp
2330Option
2331.Fl a
2332causes the
2333.Ic ulimit
2334command to display all resources.
2335The parameter
2336.Ar limit
2337is not acceptable in this mode.
2338.Pp
2339The remaining options specify which resource value is to be
2340displayed or modified.
2341They are mutually exclusive.
2342.Bl -tag -width indent
2343.It Fl b Ar sbsize
2344The maximum size of socket buffer usage, in bytes.
2345.It Fl c Ar coredumpsize
2346The maximal size of core dump files, in 512-byte blocks.
2347.It Fl d Ar datasize
2348The maximal size of the data segment of a process, in kilobytes.
2349.It Fl f Ar filesize
2350The maximal size of a file, in 512-byte blocks.
2351.It Fl l Ar lockedmem
2352The maximal size of memory that can be locked by a process, in
2353kilobytes.
2354.It Fl m Ar memoryuse
2355The maximal resident set size of a process, in kilobytes.
2356.It Fl n Ar nofiles
2357The maximal number of descriptors that could be opened by a process.
2358.It Fl p Ar pseudoterminals
2359The maximal number of pseudo-terminals for this user ID.
2360.It Fl s Ar stacksize
2361The maximal size of the stack segment, in kilobytes.
2362.It Fl t Ar time
2363The maximal amount of CPU time to be used by each process, in seconds.
2364.It Fl u Ar userproc
2365The maximal number of simultaneous processes for this user ID.
2366.It Fl v Ar virtualmem
2367The maximal virtual size of a process, in kilobytes.
2368.It Fl w Ar swapuse
2369The maximum amount of swap space reserved or used for this user ID,
2370in kilobytes.
2371.El
2372.It Ic umask Oo Fl S Oc Op Ar mask
2373Set the file creation mask (see
2374.Xr umask 2 )
2375to the octal or symbolic (see
2376.Xr chmod 1 )
2377value specified by
2378.Ar mask .
2379If the argument is omitted, the current mask value is printed.
2380If the
2381.Fl S
2382option is specified, the output is symbolic, otherwise the output is octal.
2383.It Ic unalias Oo Fl a Oc Op Ar name ...
2384The specified alias names are removed.
2385If
2386.Fl a
2387is specified, all aliases are removed.
2388.It Ic unset Oo Fl fv Oc Ar name ...
2389The specified variables or functions are unset and unexported.
2390If the
2391.Fl v
2392option is specified or no options are given, the
2393.Ar name
2394arguments are treated as variable names.
2395If the
2396.Fl f
2397option is specified, the
2398.Ar name
2399arguments are treated as function names.
2400.It Ic wait Op Ar job
2401Wait for the specified
2402.Ar job
2403to complete and return the exit status of the last process in the
2404.Ar job .
2405If the argument is omitted, wait for all jobs to complete
2406and return an exit status of zero.
2407.El
2408.Ss Commandline Editing
2409When
2410.Nm
2411is being used interactively from a terminal, the current command
2412and the command history
2413(see
2414.Ic fc
2415in
2416.Sx Built-in Commands )
2417can be edited using
2418.Nm vi Ns -mode
2419command line editing.
2420This mode uses commands similar
2421to a subset of those described in the
2422.Xr vi 1
2423man page.
2424The command
2425.Dq Li "set -o vi"
2426(or
2427.Dq Li "set -V" )
2428enables
2429.Nm vi Ns -mode
2430editing and places
2431.Nm
2432into
2433.Nm vi
2434insert mode.
2435With
2436.Nm vi Ns -mode
2437enabled,
2438.Nm
2439can be switched between insert mode and command mode by typing
2440.Aq ESC .
2441Hitting
2442.Aq return
2443while in command mode will pass the line to the shell.
2444.Pp
2445Similarly, the
2446.Dq Li "set -o emacs"
2447(or
2448.Dq Li "set -E" )
2449command can be used to enable a subset of
2450.Nm emacs Ns -style
2451command line editing features.
2452.Sh ENVIRONMENT
2453The following environment variables affect the execution of
2454.Nm :
2455.Bl -tag -width ".Ev LANGXXXXXX"
2456.It Ev ENV
2457Initialization file for interactive shells.
2458.It Ev LANG , Ev LC_*
2459Locale settings.
2460These are inherited by children of the shell,
2461and is used in a limited manner by the shell itself.
2462.It Ev PWD
2463An absolute pathname for the current directory,
2464possibly containing symbolic links.
2465This is used and updated by the shell.
2466.It Ev TERM
2467The default terminal setting for the shell.
2468This is inherited by children of the shell, and is used in the history
2469editing modes.
2470.El
2471.Pp
2472Additionally, all environment variables are turned into shell variables
2473at startup,
2474which may affect the shell as described under
2475.Sx Special Variables .
2476.Sh EXIT STATUS
2477Errors that are detected by the shell, such as a syntax error, will
2478cause the shell to exit with a non-zero exit status.
2479If the shell is not an interactive shell, the execution of the shell
2480file will be aborted.
2481Otherwise the shell will return the exit status of the last command
2482executed, or if the
2483.Ic exit
2484builtin is used with a numeric argument, it
2485will return the argument.
2486.Sh SEE ALSO
2487.Xr builtin 1 ,
2488.Xr chsh 1 ,
2489.Xr echo 1 ,
2490.Xr ed 1 ,
2491.Xr emacs 1 ,
2492.Xr kill 1 ,
2493.Xr printf 1 ,
2494.Xr pwd 1 ,
2495.Xr test 1 ,
2496.Xr vi 1 ,
2497.Xr execve 2 ,
2498.Xr getrlimit 2 ,
2499.Xr umask 2 ,
2500.Xr editrc 5
2501.Sh HISTORY
2502A
2503.Nm
2504command, the Thompson shell, appeared in
2505.At v1 .
2506It was superseded in
2507.At v7
2508by the Bourne shell, which inherited the name
2509.Nm .
2510.Pp
2511This version of
2512.Nm
2513was rewritten in 1989 under the
2514.Bx
2515license after the Bourne shell from
2516.At V.4 .
2517.Sh AUTHORS
2518This version of
2519.Nm
2520was originally written by
2521.An Kenneth Almquist .
2522.Sh BUGS
2523The
2524.Nm
2525utility does not recognize multibyte characters.
2526