Thu Mar 31 16:18:23 2016 UTC ()
Document the NETBSD_SHELL variable, the enhancements to export,
the posix option, and a whole bunch of miscellaneous updates and
corrections. (from kre@)


(christos)
diff -r1.119 -r1.120 src/bin/sh/sh.1

cvs diff -r1.119 -r1.120 src/bin/sh/sh.1 (switch to unified diff)

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