1 .\" $NetBSD: sh.1,v 1.115 2015/05/26 21:35:15 christos Exp $
2 .\" Copyright (c) 1991, 1993
3 .\" The Regents of the University of California. All rights reserved.
5 .\" This code is derived from software contributed to Berkeley by
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
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
32 .\" @(#)sh.1 8.6 (Berkeley) 5/4/95
39 .Nd command interpreter (shell)
43 .Op Fl aCefnuvxIimqVEb
44 .Op Cm +aCefnuvxIimqVEb
47 .Op Fl o Ar option_name
48 .Op Cm +o Ar option_name
51 .Op Ar command_file Oo Ar argument ... Oc
56 .Op Fl abCEefnuvxIimpqV
57 .Op Cm +abCEefnuvxIimpqV
60 .Op Fl o Ar option_name
61 .Op Cm +o Ar option_name
65 .Op Ar command_name Oo Ar argument ... Oc
70 .Op Fl abCEefnuvxIimpqV
71 .Op Cm +abCEefnuvxIimpqV
74 .Op Fl o Ar option_name
75 .Op Cm +o Ar option_name
82 is the standard command interpreter for the system.
83 The current version of
85 is in the process of being changed to conform with the
87 1003.2 and 1003.2a specifications for the shell.
89 features which make it appear similar in some respects to the Korn shell,
90 but it is not a Korn shell clone (see
92 Only features designated by
94 plus a few Berkeley extensions, are being incorporated into this shell.
97 .\" conformance by the time 4.4 BSD is released.
98 This man page is not intended
99 to be a tutorial or a complete specification of the shell.
101 The shell is a command that reads lines from either a file or the
102 terminal, interprets them, and generally executes other commands.
103 It is the program that is running when a user logs into the system
104 (although a user can select a different shell with the
107 The shell implements a language that has flow control
108 constructs, a macro facility that provides a variety of features in
109 addition to data storage, along with built in history and line editing
111 It incorporates many features to aid interactive use and
112 has the advantage that the interpretative language is common to both
113 interactive and non-interactive use (shell scripts).
115 can be typed directly to the running shell or can be put into a file and
116 the file can be executed directly by the shell.
118 If no arguments are present and if the standard input of the shell
119 is connected to a terminal (or if the
124 option is not present, the shell is considered an interactive shell.
125 An interactive shell generally prompts before each command and handles
126 programming and command errors differently (as described below).
128 the shell inspects argument 0, and if it begins with a dash
130 the shell is also considered
132 This is normally done automatically by the system
133 when the user first logs in.
134 A login shell first reads commands
140 If the environment variable
142 is set on entry to a shell, or is set in the
144 of a login shell, the shell next reads
145 commands from the file named in
147 Therefore, a user should place commands that are to be executed only at
150 file, and commands that are executed for every shell inside the
155 variable to some file, place the following line in your
157 of your home directory
159 .Dl ENV=$HOME/.shinit; export ENV
163 any filename you wish.
166 file is read for every invocation of the shell, including shell scripts
167 and non-interactive shells, the following paradigm is useful for
168 restricting commands in the
170 file to interactive invocations.
171 Place commands within the
175 below (these commands are described later):
177 .Bl -item -compact -offset indent
180 .Bl -item -compact -offset indent
182 .Li # commands for interactive use only
190 If command line arguments besides the options have been specified, then
191 the shell treats the first argument as the name of a file from which to
192 read commands (a shell script), and the remaining arguments are set as the
193 positional parameters of the shell ($1, $2, etc).
195 reads commands from its standard input.
196 .Ss Argument List Processing
197 All of the single letter options have a corresponding name that can be
198 used as an argument to the
203 name is provided next to the single letter option in
204 the description below.
207 turns the option on, while using a plus
210 The following options can be set from the command line or
213 built-in (described later).
214 .Bl -tag -width aaaallexportfoo -offset indent
215 .It Fl a Em allexport
216 Export all variables assigned to.
218 Read commands from the
220 operand instead of from the standard input.
221 Special parameter 0 will be set from the
223 operand and the positional parameters ($1, $2, etc.)
224 set from the remaining argument operands.
225 .It Fl C Em noclobber
226 Don't overwrite existing files with
229 If not interactive, exit immediately if any untested command fails.
230 The exit status of a command is considered to be
231 explicitly tested if the command is used to control an
237 or if the command is the left hand operand of an
243 Disable pathname expansion.
245 If not interactive, read commands but do not execute them.
246 This is useful for checking the syntax of shell scripts.
248 Write a message to standard error when attempting to expand a variable
249 that is not set, and if the shell is not interactive, exit immediately.
251 The shell writes its input to standard error as it is read.
252 Useful for debugging.
254 Write each command to standard error (preceded by a
256 before it is executed.
257 Useful for debugging.
258 .It Fl q Em quietprofile
263 options have been set, do not apply them when reading
264 initialization files, these being
267 and the file specified by the
269 environment variable.
270 .It Fl I Em ignoreeof
271 Ignore EOFs from input when interactive.
272 .It Fl i Em interactive
273 Force the shell to behave interactively.
275 Turn on job control (set automatically when interactive).
277 Read commands from standard input (set automatically if no file arguments
279 This option has no effect when set after the shell has
280 already started running (i.e. with
285 command line editor (disables
289 .Sx Command Line Editing
292 Enable the built-in emacs style
293 command line editor (disables
297 .Sx Command Line Editing
300 Enable asynchronous notification of background job completion.
303 Do not attempt to reset effective uid if it does not match uid.
304 This is not set by default to help avoid incorrect usage by setuid
309 .It "\ \ " Em cdprint
310 Make an interactive shell always print the new directory name when
314 .It "\ \ " Em tabcomplete
315 Enables filename completion in the command line editor.
316 Typing a tab character will extend the current input word to match a
318 If more than one filename matches it is only extended to be the common prefix.
319 Typing a second tab character will list all the matching names.
320 One of the editing modes, either
324 must be enabled for this to work.
326 .Ss Lexical Structure
327 The shell reads input in terms of lines from a file and breaks it up into
328 words at whitespace (blanks and tabs), and at certain sequences of
329 characters that are special to the shell called
331 There are two types of operators: control operators and redirection
332 operators (their meaning is discussed later).
333 Following is a list of operators:
334 .Bl -ohang -offset indent
335 .It "Control operators:"
336 .Dl \*[Am] \*[Am]\*[Am] \&( \&) \&; ;; | || \*[Lt]newline\*[Gt]
337 .It "Redirection operators:"
338 .Dl \*[Lt] \*[Gt] \*[Gt]| \*[Lt]\*[Lt] \*[Gt]\*[Gt] \*[Lt]\*[Am] \*[Gt]\*[Am] \*[Lt]\*[Lt]- \*[Lt]\*[Gt]
341 Quoting is used to remove the special meaning of certain characters or
342 words to the shell, such as operators, whitespace, or keywords.
343 There are three types of quoting: matched single quotes,
344 matched double quotes, and backslash.
346 A backslash preserves the literal meaning of the following
347 character, with the exception of
349 A backslash preceding a
351 is treated as a line continuation.
353 Enclosing characters in single quotes preserves the literal meaning of all
354 the characters (except single quotes, making it impossible to put
355 single quotes in a single-quoted string).
357 Enclosing characters within double quotes preserves the literal
358 meaning of all characters except dollar sign
364 The backslash inside double quotes is historically weird, and serves to
365 quote only the following characters:
366 .Dl $ ` \*q \e \*[Lt]newline\*[Gt] .
367 Otherwise it remains literal.
369 Reserved words are words that have special meaning to the
370 shell and are recognized at the beginning of a line and
371 after a control operator.
372 The following are reserved words:
373 .Bl -column while while while while while -offset indent
374 .It ! Ta elif Ta fi Ta while Ta case
375 .It else Ta for Ta then Ta { Ta }
376 .It do Ta done Ta until Ta if Ta esac
379 Their meaning is discussed later.
381 An alias is a name and corresponding value set using the
384 Whenever a reserved word may occur (see above),
385 and after checking for reserved words, the shell
386 checks the word to see if it matches an alias.
387 If it does, it replaces it in the input stream with its value.
388 For example, if there is an alias called
394 .Dl lf foobar Aq return
398 .Dl ls -F foobar Aq return
400 Aliases provide a convenient way for naive users to create shorthands for
401 commands without having to learn how to create functions with arguments.
402 They can also be used to create lexically obscure code.
403 This use is discouraged.
405 The shell interprets the words it reads according to a language, the
406 specification of which is outside the scope of this man page (refer to the
410 Essentially though, a line is read and if the first
411 word of the line (or after a control operator) is not a reserved word,
412 then the shell has recognized a simple command.
414 command or some other special construct may have been recognized.
416 If a simple command has been recognized, the shell performs
417 the following actions:
418 .Bl -enum -offset indent
420 Leading words of the form
422 are stripped off and assigned to the environment of the simple command.
423 Redirection operators and their arguments (as described below) are
424 stripped off and saved for processing.
426 The remaining words are expanded as described in the
429 and the first remaining word is considered the command name and the
431 The remaining words are considered the arguments of the command.
432 If no command name resulted, then the
434 variable assignments recognized in item 1 affect the current shell.
436 Redirections are performed as described in the next section.
439 Redirections are used to change where a command reads its input or sends
441 In general, redirections open, close, or duplicate an
442 existing reference to a file.
443 The overall format used for redirection is:
445 .Dl [n] Va redir-op Ar file
449 is one of the redirection operators mentioned previously.
450 Following is a list of the possible redirections.
453 is an optional number, as in
457 that refers to a file descriptor.
458 .Bl -tag -width aaabsfiles -offset indent
459 .It [n] Ns \*[Gt] file
460 Redirect standard output (or n) to file.
461 .It [n] Ns \*[Gt]| file
462 Same, but override the
465 .It [n] Ns \*[Gt]\*[Gt] file
466 Append standard output (or n) to file.
467 .It [n] Ns \*[Lt] file
468 Redirect standard input (or n) from file.
469 .It [n1] Ns \*[Lt]\*[Am] Ns n2
470 Duplicate standard input (or n1) from file descriptor n2.
471 .It [n] Ns \*[Lt]\*[Am]-
472 Close standard input (or n).
473 .It [n1] Ns \*[Gt]\*[Am] Ns n2
474 Duplicate standard output (or n1) to n2.
475 .It [n] Ns \*[Gt]\*[Am]-
476 Close standard output (or n).
477 .It [n] Ns \*[Lt]\*[Gt] file
478 Open file for reading and writing on standard input (or n).
481 The following redirection is often called a
483 .Bl -item -offset indent
485 .Li [n]\*[Lt]\*[Lt] delimiter
486 .Dl here-doc-text ...
490 All the text on successive lines up to the delimiter, or to an EOF, is
491 saved away and made available to the command on standard input, or file
492 descriptor n if it is specified.
493 If the delimiter as specified on the initial line is
494 quoted, then the here-doc-text is treated literally; otherwise, the text is
495 subjected to parameter expansion, command substitution, and arithmetic
496 expansion as described in the
503 then leading tabs in the here-doc-text are stripped.
504 .Ss Search and Execution
505 There are three types of commands: shell functions, built-in commands, and
506 normal programs -- and the command is searched for (by name) in that order.
507 They each are executed in a different way.
509 When a shell function is executed, all of the shell positional parameters
510 (except $0, which remains unchanged) are set to the arguments of the shell
512 The variables which are explicitly placed in the environment of
513 the command (by placing assignments to them before the function name) are
514 made local to the function and are set to the values given.
515 Then the command given in the function definition is executed.
516 The positional parameters are restored to their original values
517 when the command completes.
518 This all occurs within the current shell.
520 Shell built-ins are executed internally to the shell, without spawning a
523 Otherwise, if the command name doesn't match a function or built-in, the
524 command is searched for as a normal program in the file system (as
525 described in the next section).
526 When a normal program is executed, the shell runs the program,
527 passing the arguments and the environment to the program.
528 If the program is not a normal executable file (i.e., if it does
529 not begin with the "magic number" whose
531 representation is "#!", so
535 then) the shell will interpret the program in a subshell.
536 The child shell will reinitialize itself in this case,
537 so that the effect will be as if a
538 new shell had been invoked to handle the ad-hoc shell script, except that
539 the location of hashed commands located in the parent shell will be
540 remembered by the child.
542 Note that previous versions of this document and the source code itself
543 misleadingly and sporadically refer to a shell script without a magic
544 number as a "shell procedure".
546 When locating a command, the shell first looks to see if it has a shell
547 function by that name.
548 Then it looks for a built-in command by that name.
549 If a built-in command is not found, one of two things happen:
552 Command names containing a slash are simply executed without performing
555 The shell searches each entry in
557 in turn for the command.
560 variable should be a series of entries separated by colons.
561 Each entry consists of a directory name.
562 The current directory may be indicated
563 implicitly by an empty directory name, or explicitly by a single period.
565 .Ss Command Exit Status
566 Each command has an exit status that can influence the behavior
567 of other shell commands.
568 The paradigm is that a command exits
569 with zero for normal or success, and non-zero for failure,
570 error, or a false indication.
571 The man page for each command
572 should indicate the various exit codes and what they mean.
573 Additionally, the built-in commands return exit codes, as does
574 an executed shell function.
576 If a command consists entirely of variable assignments then the
577 exit status of the command is that of the last command substitution
580 Complex commands are combinations of simple commands with control
581 operators or reserved words, together creating a larger complex command.
582 More generally, a command is one of the following:
589 list or compound-list
596 Unless otherwise stated, the exit status of a command is that of the last
597 simple command executed by the command.
599 A pipeline is a sequence of one or more commands separated
600 by the control operator |.
601 The standard output of all but
602 the last command is connected to the standard input
604 The standard output of the last
605 command is inherited from the shell, as usual.
607 The format for a pipeline is:
609 .Dl [!] command1 [ | command2 ...]
611 The standard output of command1 is connected to the standard input of
613 The standard input, standard output, or both of a command is
614 considered to be assigned by the pipeline before any redirection specified
615 by redirection operators that are part of the command.
617 If the pipeline is not in the background (discussed later), the shell
618 waits for all commands to complete.
620 If the reserved word ! does not precede the pipeline, the exit status is
621 the exit status of the last command specified in the pipeline.
622 Otherwise, the exit status is the logical NOT of the exit status of the
624 That is, if the last command returns zero, the exit status
625 is 1; if the last command returns greater than zero, the exit status is
628 Because pipeline assignment of standard input or standard output or both
629 takes place before redirection, it can be modified by redirection.
632 .Dl $ command1 2\*[Gt]\*[Am]1 | command2
634 sends both the standard output and standard error of command1
635 to the standard input of command2.
639 terminator causes the preceding AND-OR-list (described
640 next) to be executed sequentially; a \*[Am] causes asynchronous execution of
641 the preceding AND-OR-list.
643 Note that unlike some other shells, each process in the pipeline is a
644 child of the invoking shell (unless it is a shell built-in, in which case
645 it executes in the current shell -- but any effect it has on the
646 environment is wiped).
647 .Ss Background Commands -- \*[Am]
648 If a command is terminated by the control operator ampersand (\*[Am]), the
649 shell executes the command asynchronously -- that is, the shell does not
650 wait for the command to finish before executing the next command.
652 The format for running a command in background is:
654 .Dl command1 \*[Am] [command2 \*[Am] ...]
656 If the shell is not interactive, the standard input of an asynchronous
659 .Ss Lists -- Generally Speaking
660 A list is a sequence of zero or more commands separated by newlines,
661 semicolons, or ampersands, and optionally terminated by one of these three
663 The commands in a list are executed in the order they are written.
664 If command is followed by an ampersand, the shell starts the
665 command and immediately proceed onto the next command; otherwise it waits
666 for the command to terminate before proceeding to the next one.
667 .Ss Short-Circuit List Operators
671 are AND-OR list operators.
673 executes the first command, and then executes the second command if and only
674 if the exit status of the first command is zero.
676 is similar, but executes the second command if and only if the exit status
677 of the first command is nonzero.
681 both have the same priority.
682 Note that these operators are left-associative, so
683 .Dq true || echo bar \*[Am]\*[Am] echo baz
687 This is not the way it works in C.
688 Also, if you forget the left-hand side (for example when continuing lines but
689 forgetting to use a backslash) it defaults to a true statement.
690 This behavior is not useful and should not be relied upon.
691 .Ss Flow-Control Constructs -- if, while, for, case
692 The syntax of the if command is
693 .Bd -literal -offset indent
702 The syntax of the while command is
703 .Bd -literal -offset indent
709 The two lists are executed repeatedly while the exit status of the
711 The until command is similar, but has the word
712 until in place of while, which causes it to
713 repeat until the exit status of the first list is zero.
715 The syntax of the for command is
716 .Bd -literal -offset indent
717 for variable in word ...
722 The words are expanded, and then the list is executed repeatedly with the
723 variable set to each word in turn.
724 do and done may be replaced with
729 The syntax of the break and continue command is
730 .Bd -literal -offset indent
735 Break terminates the num innermost for or while loops.
736 Continue continues with the next iteration of the innermost loop.
737 These are implemented as built-in commands.
739 The syntax of the case command is
740 .Bd -literal -offset indent
747 The pattern can actually be one or more patterns (see
749 described later), separated by
752 .Ss Grouping Commands Together
753 Commands may be grouped by writing either
761 The first of these executes the commands in a subshell.
762 Built-in commands grouped into a (list) will not affect the current shell.
763 The second form does not fork another shell so is slightly more efficient.
764 Grouping commands together this way allows you to redirect
765 their output as though they were one program:
766 .Bd -literal -offset indent
767 { echo -n \*q hello \*q ; echo \*q world" ; } \*[Gt] greeting
772 must follow a control operator (here,
774 so that it is recognized as a reserved word and not as another command argument.
776 The syntax of a function definition is
780 A function definition is an executable statement; when executed it
781 installs a function named name and returns an exit status of zero.
782 The command is normally a list enclosed between
787 Variables may be declared to be local to a function by using a local
789 This should appear as the first statement of a function, and the syntax is
791 .Dl local [ variable | - ] ...
794 is implemented as a built-in command.
796 When a variable is made local, it inherits the initial value and exported
797 and read-only flags from the variable with the same name in the surrounding
798 scope, if there is one.
799 Otherwise, the variable is initially unset.
800 The shell uses dynamic scoping, so that if you make the variable x local to
801 function f, which then calls function g, references to the variable x made
802 inside g will refer to the variable x declared inside f, not to the global
805 The only special parameter that can be made local is
809 local causes any shell options that are changed via the set command inside the
810 function to be restored to their original values when the function
813 The syntax of the return command is
815 .Dl return [ exitstatus ]
817 It terminates the currently executing function.
818 Return is implemented as a built-in command.
819 .Ss Variables and Parameters
820 The shell maintains a set of parameters.
821 A parameter denoted by a name is called a variable.
822 When starting up, the shell turns all the environment
823 variables into shell variables.
824 New variables can be set using the form
828 Variables set by the user must have a name consisting solely of
829 alphabetics, numerics, and underscores - the first of which must not be
831 A parameter can also be denoted by a number or a special
832 character as explained below.
833 .Ss Positional Parameters
834 A positional parameter is a parameter denoted by a number (n \*[Gt] 0).
835 The shell sets these initially to the values of its command line arguments
836 that follow the name of the shell script.
839 built-in can also be used to set or reset them.
840 .Ss Special Parameters
841 A special parameter is a parameter denoted by one of the following special
843 The value of the parameter is listed next to its character.
844 .Bl -tag -width thinhyphena
846 Expands to the positional parameters, starting from one.
848 expansion occurs within a double-quoted string it expands to a single
849 field with the value of each parameter separated by the first character of
858 Expands to the positional parameters, starting from one.
859 When the expansion occurs within double quotes, each positional
860 parameter expands as a separate argument.
861 If there are no positional parameters, the
862 expansion of @ generates zero arguments, even when @ is
864 What this basically means, for example, is
875 .Dl \*q abc \*q \ \*q def\ ghi \*q
878 Expands to the number of positional parameters.
880 Expands to the exit status of the most recent pipeline.
882 Expands to the current option flags (the single-letter
883 option names concatenated into a string) as specified on
884 invocation, by the set built-in command, or implicitly
887 Expands to the process ID of the invoked shell.
888 A subshell retains the same value of $ as its parent.
890 Expands to the process ID of the most recent background
891 command executed from the current shell.
892 For a pipeline, the process ID is that of the last command in the pipeline.
894 Expands to the name of the shell or shell script.
897 This section describes the various expansions that are performed on words.
898 Not all expansions are performed on every word, as explained later.
900 Tilde expansions, parameter expansions, command substitutions, arithmetic
901 expansions, and quote removals that occur within a single word expand to a
903 It is only field splitting or pathname expansion that can
904 create multiple fields from a single word.
905 The single exception to this
906 rule is the expansion of the special parameter @ within double quotes, as
909 The order of word expansion is:
912 Tilde Expansion, Parameter Expansion, Command Substitution,
913 Arithmetic Expansion (these all occur at the same time).
915 Field Splitting is performed on fields
916 generated by step (1) unless the
920 Pathname Expansion (unless set
927 The $ character is used to introduce parameter expansion, command
928 substitution, or arithmetic evaluation.
929 .Ss Tilde Expansion (substituting a user's home directory)
930 A word beginning with an unquoted tilde character (~) is
931 subjected to tilde expansion.
932 All the characters up to
933 a slash (/) or the end of the word are treated as a username
934 and are replaced with the user's home directory.
935 If the username is missing (as in
937 the tilde is replaced with the value of the
939 variable (the current user's home directory).
940 .Ss Parameter Expansion
941 The format for parameter expansion is as follows:
945 where expression consists of all characters until the matching
949 escaped by a backslash or within a quoted string, and characters in
950 embedded arithmetic expansions, command substitutions, and variable
951 expansions, are not examined in determining the matching
954 The simplest form for parameter expansion is:
958 The value, if any, of parameter is substituted.
960 The parameter name or symbol can be enclosed in braces, which are
961 optional except for positional parameters with more than one digit or
962 when parameter is followed by a character that could be interpreted as
964 If a parameter expansion occurs inside double quotes:
967 Pathname expansion is not performed on the results of the expansion.
969 Field splitting is not performed on the results of the
970 expansion, with the exception of the special rules for @.
973 In addition, a parameter expansion can be modified by using one of the
977 is omitted in the following modifiers, then the expansion is applied only
978 to unset parameters, not null ones.
979 .Bl -tag -width aaparameterwordaaaaa
980 .It ${parameter:-word}
982 If parameter is unset or null, the expansion of word
983 is substituted; otherwise, the value of parameter is substituted.
984 .It ${parameter:=word}
985 Assign Default Values.
986 If parameter is unset or null, the expansion of
987 word is assigned to parameter.
988 In all cases, the final value of parameter is substituted.
989 Only variables, not positional parameters or special
990 parameters, can be assigned in this way.
991 .It ${parameter:?[word]}
992 Indicate Error if Null or Unset.
993 If parameter is unset or null, the
994 expansion of word (or a message indicating it is unset if word is omitted)
995 is written to standard error and the shell exits with a nonzero exit status.
996 Otherwise, the value of parameter is substituted.
997 An interactive shell need not exit.
998 .It ${parameter:+word}
999 Use Alternative Value.
1000 If parameter is unset or null, null is
1001 substituted; otherwise, the expansion of word is substituted.
1004 The length in characters of the value of parameter.
1007 The following four varieties of parameter expansion provide for substring
1009 In each case, pattern matching notation (see
1010 .Sx Shell Patterns ) ,
1011 rather than regular expression notation, is used to evaluate the patterns.
1012 If parameter is * or @, the result of the expansion is unspecified.
1013 Enclosing the full parameter expansion string in double quotes does not
1014 cause the following four varieties of pattern characters to be quoted,
1015 whereas quoting characters within the braces has this effect.
1016 .Bl -tag -width aaparameterwordaaaaa
1017 .It ${parameter%word}
1018 Remove Smallest Suffix Pattern.
1019 The word is expanded to produce a pattern.
1020 The parameter expansion then results in parameter, with the
1021 smallest portion of the suffix matched by the pattern deleted.
1022 .It ${parameter%%word}
1023 Remove Largest Suffix Pattern.
1024 The word is expanded to produce a pattern.
1025 The parameter expansion then results in parameter, with the largest
1026 portion of the suffix matched by the pattern deleted.
1027 .It ${parameter#word}
1028 Remove Smallest Prefix Pattern.
1029 The word is expanded to produce a pattern.
1030 The parameter expansion then results in parameter, with the
1031 smallest portion of the prefix matched by the pattern deleted.
1032 .It ${parameter##word}
1033 Remove Largest Prefix Pattern.
1034 The word is expanded to produce a pattern.
1035 The parameter expansion then results in parameter, with the largest
1036 portion of the prefix matched by the pattern deleted.
1038 .Ss Command Substitution
1039 Command substitution allows the output of a command to be substituted in
1040 place of the command name itself.
1041 Command substitution occurs when the command is enclosed as follows:
1053 The shell expands the command substitution by executing command in a
1054 subshell environment and replacing the command substitution with the
1055 standard output of the command, removing sequences of one or more
1057 at the end of the substitution.
1061 the end of the output are not removed; however, during field splitting,
1062 they may be translated into
1064 depending on the value of
1066 and quoting that is in effect.)
1067 .Ss Arithmetic Expansion
1068 Arithmetic expansion provides a mechanism for evaluating an arithmetic
1069 expression and substituting its value.
1070 The format for arithmetic expansion is as follows:
1074 The expression is treated as if it were in double quotes, except
1075 that a double quote inside the expression is not treated specially.
1076 The shell expands all tokens in the expression for parameter expansion,
1077 command substitution, and quote removal.
1079 Next, the shell treats this as an arithmetic expression and
1080 substitutes the value of the expression.
1082 Arithmetic expressions use a syntax similar to that
1083 of the C language, and are evaluated using the
1085 data type (this is an extension to
1090 Shell variables may be referenced by name inside an arithmetic
1091 expression, without needing a
1094 .Ss White Space Splitting (Field Splitting)
1095 After parameter expansion, command substitution, and
1096 arithmetic expansion the shell scans the results of
1097 expansions and substitutions that did not occur in double quotes for
1098 field splitting and multiple fields can result.
1100 The shell treats each character of the
1102 as a delimiter and use the delimiters to split the results of parameter
1103 expansion and command substitution into fields.
1105 Non-whitespace characters in
1107 are treated strictly as parameter terminators.
1108 So adjacent non-whitespace
1110 characters will produce empty parameters.
1114 is unset it is assumed to contain space, tab, and newline.
1115 .Ss Pathname Expansion (File Name Generation)
1118 flag is set, file name generation is performed after word splitting is
1120 Each word is viewed as a series of patterns, separated by slashes.
1121 The process of expansion replaces the word with the names of all
1122 existing files whose names can be formed by replacing each pattern with a
1123 string that matches the specified pattern.
1124 There are two restrictions on
1125 this: first, a pattern cannot match a string containing a slash, and
1126 second, a pattern cannot match a string starting with a period unless the
1127 first character of the pattern is a period.
1128 The next section describes the
1129 patterns used for both Pathname Expansion and the
1133 A pattern consists of normal characters, which match themselves,
1134 and meta-characters.
1135 The meta-characters are
1141 These characters lose their special meanings if they are quoted.
1142 When command or variable substitution is performed
1143 and the dollar sign or backquotes are not double-quoted,
1144 the value of the variable or the output of
1145 the command is scanned for these characters and they are turned into
1150 matches any string of characters.
1153 matches any single character.
1156 introduces a character class.
1157 The end of the character class is indicated by a right bracket
1165 rather than introducing a character class.
1166 A character class matches any of the characters between the square brackets.
1167 A named class of characters (see
1169 may be specified by surrounding the name with
1175 is a shell pattern that matches a single letter.
1176 A range of characters may be specified using a minus sign
1178 The character class may be complemented
1179 by making an exclamation mark
1181 the first character of the character class.
1185 in a character class, make it the first character listed (after the
1190 make it the first or last character listed.
1192 This section lists the built-in commands which are built-in because they
1193 need to perform some operation that can't be performed by a separate
1195 In addition to these, there are several other commands that may
1196 be built in for efficiency (e.g.
1202 .It : [ Ar arg ... ]
1203 A null command that returns a 0 (true) exit value.
1204 Any arguments are ignored.
1206 The dot command reads and executes the commands from the specified
1208 in the current shell environment.
1209 The file does not need to be executable and is looked up from the directories
1212 variable if it does not contain a directory separator
1214 The return command can be used for a premature return from the sourced file.
1216 The POSIX standard is unclear on how loop control keywords (break
1217 and continue) behave across a dot command boundary.
1218 This implementation allows them to control loops surrounding the dot command,
1219 but obviously such behavior should not be relied on.
1220 .It alias Op Ar name Ns Op Ar "=string ..."
1223 is specified, the shell defines the alias
1229 is specified, the value of the alias
1232 With no arguments, the
1235 names and values of all defined aliases (see
1237 .It bg [ Ar job ] ...
1238 Continue the specified jobs (or the current job if no
1239 jobs are given) in the background.
1240 .It command Oo Fl p Oc Oo Fl v Oc Oo Fl V Oc Ar command Oo Ar arg ... Oc
1241 Execute the specified command but ignore shell functions when searching
1243 (This is useful when you
1244 have a shell function with the same name as a built-in command.)
1247 search for command using a
1249 that guarantees to find all the standard utilities.
1251 Do not execute the command but
1252 search for the command and print the resolution of the
1254 This is the same as the
1258 Do not execute the command but
1259 search for the command and print the absolute pathname
1260 of utilities, the name for built-ins or the expansion of aliases.
1262 .It cd Oo Fl P Oc Op Ar directory Op Ar replace
1263 Switch to the specified directory (default
1267 is specified, then the new directory name is generated by replacing
1268 the first occurrence of
1270 in the current directory name with
1276 then the current working directory is changed to the previous current
1277 working directory as set in
1279 Otherwise if an entry for
1281 appears in the environment of the
1283 command or the shell variable
1285 is set and the directory name does not begin with a slash,
1286 or its first (or only) component isn't dot or dot dot,
1287 then the directories listed in
1289 will be searched for the specified directory.
1292 is the same as that of
1297 option instructs the shell to update
1299 with the specified physical directory path and change to that directory.
1300 This is the default.
1302 When the directory changes, the variable
1304 is set to the working directory before the change.
1306 Some shells also support a
1308 option, which instructs the shell to update
1310 with the logical path and to change the current directory
1312 This is not supported.
1314 In an interactive shell, the
1316 command will print out the name of the
1317 directory that it actually switched to if this is different from the name
1319 These may be different either because the
1321 mechanism was used or because a symbolic link was crossed.
1322 .It eval Ar string ...
1323 Concatenate all the arguments with spaces.
1324 Then re-parse and execute the command.
1325 .It exec Op Ar command arg ...
1326 Unless command is omitted, the shell process is replaced with the
1327 specified program (which must be a real program, not a shell built-in or
1329 Any redirections on the
1331 command are marked as permanent, so that they are not undone when the
1334 .It exit Op Ar exitstatus
1335 Terminate the shell process.
1338 is given it is used as the exit status of the shell; otherwise the
1339 exit status of the preceding command is used.
1340 .It export Ar name ...
1342 The specified names are exported so that they will appear in the
1343 environment of subsequent commands.
1344 The only way to un-export a variable is to unset it.
1345 The shell allows the value of a variable to be set at the
1346 same time it is exported by writing
1348 .Dl export name=value
1350 With no arguments the export command lists the names of all exported variables.
1353 option specified the output will be formatted suitably for non-interactive use.
1354 .It fc Oo Fl e Ar editor Oc Oo Ar first Oo Ar last Oc Oc
1355 .It fc Fl l Oo Fl nr Oc Oo Ar first Oo Ar last Oc Oc
1356 .It fc Fl s Oo Ar old=new Oc Oo Ar first Oc
1359 built-in lists, or edits and re-executes, commands previously entered
1360 to an interactive shell.
1363 Use the editor named by editor to edit the commands.
1364 The editor string is a command name, subject to search via the
1369 variable is used as a default when
1374 is null or unset, the value of the
1381 is used as the editor.
1383 List the commands rather than invoking an editor on them.
1384 The commands are written in the sequence indicated by
1385 the first and last operands, as affected by
1387 with each command preceded by the command number.
1389 Suppress command numbers when listing with -l.
1391 Reverse the order of the commands listed (with
1393 or edited (with neither
1398 Re-execute the command without invoking an editor.
1401 Select the commands to list or edit.
1402 The number of previous commands that
1403 can be accessed are determined by the value of the
1406 The value of first or last or both are one of the following:
1409 A positive number representing a command number; command numbers can be
1414 A negative decimal number representing the command that was executed
1415 number of commands previously.
1416 For example, \-1 is the immediately previous command.
1419 A string indicating the most recently entered command that begins with
1421 If the old=new operand is not also specified with
1423 the string form of the first operand cannot contain an embedded equal sign.
1426 The following environment variables affect the execution of fc:
1427 .Bl -tag -width HISTSIZE
1429 Name of the editor to use.
1431 The number of previous commands that are accessible.
1434 Move the specified job or the current job to the foreground.
1435 .It getopts Ar optstring var
1439 command, not to be confused with the
1444 The first argument should be a series of letters, each of which may be
1445 optionally followed by a colon to indicate that the option requires an
1447 The variable specified is set to the parsed option.
1451 command deprecates the older
1453 utility due to its handling of arguments containing whitespace.
1457 built-in may be used to obtain options and their arguments
1458 from a list of parameters.
1461 places the value of the next option from the option string in the list in
1462 the shell variable specified by
1464 and its index in the shell variable
1466 When the shell is invoked,
1468 is initialized to 1.
1469 For each option that requires an argument, the
1471 built-in will place it in the shell variable
1473 If an option is not allowed for in the
1480 is a string of recognized option letters (see
1482 If a letter is followed by a colon, the option is expected to have an
1483 argument which may or may not be separated from it by whitespace.
1484 If an option character is not found where expected,
1486 will set the variable
1493 and write output to standard error.
1494 By specifying a colon as the first character of
1496 all errors will be ignored.
1498 A nonzero value is returned when the last option is reached.
1499 If there are no remaining arguments,
1503 to the special option,
1505 otherwise, it will set
1510 The following code fragment shows how one might process the arguments
1511 for a command that can take the options
1517 which requires an argument.
1518 .Bd -literal -offset indent
1519 while getopts abc: f
1524 \e?) echo $USAGE; exit 1;;
1527 shift $(expr $OPTIND - 1)
1530 This code will accept any of the following as equivalent:
1531 .Bd -literal -offset indent
1532 cmd \-acarg file file
1533 cmd \-a \-c arg file file
1534 cmd \-carg -a file file
1535 cmd \-a \-carg \-\- file file
1537 .It hash Fl rv Ar command ...
1538 The shell maintains a hash table which remembers the
1539 locations of commands.
1540 With no arguments whatsoever,
1543 command prints out the contents of this table.
1544 Entries which have not been looked at since the last
1546 command are marked with an asterisk; it is possible for these entries
1551 command removes the specified commands from the hash table (unless
1552 they are functions) and then locates them.
1555 option, hash prints the locations of the commands as it finds them.
1558 option causes the hash command to delete all the entries in the hash table
1559 except for functions.
1563 to set keybindings as defined by
1566 Print the process id's of the processes in the job.
1569 argument is omitted, the current job is used.
1571 This command lists out all the background processes
1572 which are children of the current shell process.
1574 Print the current directory.
1577 is specified the cached value (initially set from
1579 is checked to see if it refers to the current directory; if it does
1580 the value is printed.
1581 Otherwise the current directory name is found using
1583 The environment variable
1585 is set to the printed value.
1590 but note that the built-in
1592 command doesn't currently support the
1594 option and will cache (almost) the absolute path.
1599 may be changed to default to
1603 If the current directory is renamed and replaced by a symlink to the
1604 same directory, or the initial
1606 value followed a symbolic link, then the cached value may not
1607 be the absolute path.
1609 The built-in command may differ from the program of the same name because
1610 the program will use
1612 and the built-in uses a separately cached value.
1613 .It read Oo Fl p Ar prompt Oc Oo Fl r Oc Ar variable Oo Ar ... Oc
1614 The prompt is printed if the
1616 option is specified and the standard input is a terminal.
1617 Then a line is read from the standard input.
1618 The trailing newline is deleted from the
1619 line and the line is split as described in the
1621 section above, and the pieces are assigned to the variables in order.
1622 If there are more pieces than variables, the remaining pieces
1623 (along with the characters in
1625 that separated them) are assigned to the last variable.
1626 If there are more variables than pieces,
1627 the remaining variables are assigned the null string.
1630 built-in will indicate success unless EOF is encountered on input, in
1631 which case failure is returned.
1633 By default, unless the
1635 option is specified, the backslash
1637 acts as an escape character, causing the following character to be treated
1639 If a backslash is followed by a newline, the backslash and the
1640 newline will be deleted.
1641 .It readonly Ar name ...
1643 The specified names are marked as read only, so that they cannot be
1644 subsequently modified or unset.
1645 The shell allows the value of a variable
1646 to be set at the same time it is marked read only by writing
1648 .Dl readonly name=value
1650 With no arguments the readonly command lists the names of all read only
1654 option specified the output will be formatted suitably for non-interactive use.
1657 Stop executing the current function or a dot command with return value of
1659 or the value of the last executed command, if not specified.
1662 should be in the range from 0 to 255.
1664 The POSIX standard says that the results of
1666 outside a function or a dot command are unspecified.
1667 This implementation treats such a return as a no-op with a return value of 0
1669 Use the exit command instead, if you want to return from a script or exit
1671 .It set Oo { Fl options | Cm +options | Cm \-- } Oc Ar arg ...
1674 command performs three different functions.
1676 With no arguments, it lists the values of all shell variables.
1678 If options are given, it sets the specified option
1679 flags, or clears them as described in the
1680 .Sx Argument List Processing
1683 The third use of the set command is to set the values of the shell's
1684 positional parameters to the specified arguments.
1685 To change the positional
1686 parameters without changing any options, use
1688 as the first argument to set.
1689 If no arguments are present, the set command
1690 will clear all the positional parameters (equivalent to executing
1692 .It setvar Ar variable Ar value
1693 Assigns value to variable.
1694 (In general it is better to write
1695 variable=value rather than using
1698 is intended to be used in
1699 functions that assign values to variables whose names are passed as
1702 Shift the positional parameters n times.
1713 and so on, decreasing
1717 If there are zero positional parameters,
1721 .It trap Oo Ar action Oc Ar signal ...
1722 Cause the shell to parse and execute action when any of the specified
1723 signals are received.
1724 The signals are specified by signal number or as the name of the signal.
1729 or its equivalent, EXIT,
1730 the action is executed when the shell exits.
1732 may be null, which cause the specified signals to be ignored.
1737 the specified signals are set to their default action.
1738 When the shell forks off a subshell, it resets trapped (but not ignored)
1739 signals to the default action.
1740 On non-interactive shells, the
1742 command has no effect on signals that were
1743 ignored on entry to the shell.
1744 On interactive shells, the
1746 command will catch or reset signals ignored on entry.
1751 will print a list of valid signal names.
1753 without any arguments cause it to write a list of signals and their
1754 associated action to the standard output in a format that is suitable
1755 as an input to the shell that achieves the same trapping results.
1761 List trapped signals and their corresponding action
1765 Print a list of valid signals
1767 .Dl trap '' INT QUIT tstp 30
1769 Ignore signals INT QUIT TSTP USR1
1773 Print date upon receiving signal INT
1774 .It type Op Ar name ...
1775 Interpret each name as a command and print the resolution of the command
1777 Possible resolutions are:
1778 shell keyword, alias, shell built-in,
1779 command, tracked alias and not found.
1780 For aliases the alias expansion is
1781 printed; for commands and tracked aliases the complete pathname of the
1783 .It ulimit Oo Fl H \*(Ba Fl S Oc Oo Fl a \*(Ba Fl btfdscmlrpnv Oo Ar value Oc Oc
1784 Inquire about or set the hard or soft limits on processes or set new
1786 The choice between hard limit (which no process is allowed to
1787 violate, and which may not be raised once it has been lowered) and soft
1788 limit (which causes processes to be signaled but not necessarily killed,
1789 and which may be raised) is made with these flags:
1792 set or inquire about hard limits
1794 set or inquire about soft limits.
1799 is specified, the soft limit is displayed or both limits are set.
1800 If both are specified, the last one wins.
1803 The limit to be interrogated or set, then, is chosen by specifying
1804 any one of these flags:
1807 show all the current limits
1809 show or set the limit on the socket buffer size of a process (in bytes)
1811 show or set the limit on CPU time (in seconds)
1813 show or set the limit on the largest file that can be created
1814 (in 512-byte blocks)
1816 show or set the limit on the data segment size of a process (in kilobytes)
1818 show or set the limit on the stack size of a process (in kilobytes)
1820 show or set the limit on the largest core dump size that can be produced
1821 (in 512-byte blocks)
1823 show or set the limit on the total physical memory that can be
1824 in use by a process (in kilobytes)
1826 show or set the limit on how much memory a process can lock with
1830 show or set the limit on the number of threads this user can
1833 show or set the limit on the number of processes this user can
1836 show or set the limit on the number of files a process can have open at once
1838 show or set the limit on how large a process address space can be
1841 If none of these is specified, it is the limit on file size that is shown
1843 If value is specified, the limit is set to that number; otherwise
1844 the current limit is displayed.
1846 Limits of an arbitrary process can be displayed or set using the
1850 .It umask Op Ar mask
1851 Set the value of umask (see
1853 to the specified octal value.
1854 If the argument is omitted, the umask value is printed.
1855 .It unalias Oo Fl a Oc Oo Ar name Oc
1858 is specified, the shell removes that alias.
1861 is specified, all aliases are removed.
1862 .It unset Ar name ...
1863 The specified variables and functions are unset and unexported.
1864 If a given name corresponds to both a variable and a function, both
1865 the variable and the function are unset.
1867 Wait for the specified job to complete and return the exit status of the
1868 last process in the job.
1869 If the argument is omitted, wait for all jobs to
1870 complete and then return an exit status of zero.
1872 .Ss Command Line Editing
1875 is being used interactively from a terminal, the current command
1876 and the command history (see
1881 can be edited using emacs-mode or vi-mode command-line editing.
1884 enables emacs-mode editing.
1887 enables vi-mode editing and places the current shell process into
1891 .Sx Argument List Processing
1896 mode uses commands similar to a subset of those described in the
1902 can be switched between insert mode and command mode.
1907 key will throw you into command VI command mode.
1910 key while in command mode will pass the line to the shell.
1914 mode uses commands similar to a subset available in
1918 With emacs-mode enabled, special keys can be used to modify the text
1919 in the buffer using the control key.
1926 .Bl -tag -width MAILCHECK
1928 Set automatically by
1930 from the user's login directory in the password file
1932 This environment variable also functions as the default argument for the
1936 The default search path for executables.
1941 The search path used with the
1945 The current line number in the script or function.
1947 The string used to specify localization information that allows users
1948 to work with different culture-specific and language conventions.
1952 The name of a mail file, that will be checked for the arrival of new mail.
1956 The frequency in seconds that the shell checks for the arrival of mail
1957 in the files specified by the
1962 If set to 0, the check will occur at each prompt.
1966 separated list of file names, for the shell to check for incoming mail.
1967 This environment setting overrides the
1970 There is a maximum of 10 mailboxes that can be monitored at once.
1972 The primary prompt string, which defaults to
1974 unless you are the superuser, in which case it defaults to
1977 The secondary prompt string, which defaults to
1980 Output before each line when execution trace (set -x) is enabled,
1984 Input Field Separators.
1985 This is normally set to
1991 .Sx White Space Splitting
1992 section for more details.
1994 The default terminal setting for the shell.
1995 This is inherited by
1996 children of the shell, and is used in the history editing modes.
1998 The number of lines in the history buffer for the shell.
2008 Errors that are detected by the shell, such as a syntax error, will cause the
2009 shell to exit with a non-zero exit status.
2010 If the shell is not an
2011 interactive shell, the execution of the shell file will be aborted.
2013 the shell will return the exit status of the last command executed, or
2014 if the exit built-in is used with a numeric argument, it will return the
2037 It was, however, unmaintainable so we wrote this one.
2039 Setuid shell scripts should be avoided at all costs, as they are a
2040 significant security risk.
2042 PS1, PS2, and PS4 should be subject to parameter expansion before
2045 The characters generated by filename completion should probably be quoted
2046 to ensure that the filename is still valid after the input line has been