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