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