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