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