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