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