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