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