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