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