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. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
37 .\" $FreeBSD: src/bin/sh/sh.1,v 1.124 2006/10/07 16:51:16 stefanf Exp $
38 .\" $DragonFly: src/bin/sh/sh.1,v 1.9 2007/01/16 08:49:57 swildner Exp $
45 .Nd command interpreter (shell)
48 .Op Fl /+abCEefIimnPpsTuVvx
49 .Op Fl /+o Ar longname
55 utility is the standard command interpreter for the system.
56 The current version of
58 is in the process of being changed to
61 specification for the shell.
62 This version has many features which make
64 similar in some respects to the Korn shell, but it is not a Korn
65 shell clone like pdksh.
67 designated by POSIX, plus a few Berkeley extensions, are being
68 incorporated into this shell.
69 This man page is not intended to be a tutorial nor a complete
70 specification of the shell.
72 The shell is a command that reads lines from
73 either a file or the terminal, interprets them, and
74 generally executes other commands.
75 It is the program that is started when a user logs into the system,
76 although a user can select a different shell with the
80 implements a language that has flow control constructs,
81 a macro facility that provides a variety of features in
82 addition to data storage, along with built-in history and line
84 It incorporates many features to
85 aid interactive use and has the advantage that the interpretative
86 language is common to both interactive and non-interactive
88 That is, commands can be typed directly
89 to the running shell or can be put into a file,
90 which can be executed directly by the shell.
93 .\" XXX This next sentence is incredibly confusing.
95 If no arguments are present and if the standard input of the shell
96 is connected to a terminal
100 the shell is considered an interactive shell.
102 generally prompts before each command and handles programming
103 and command errors differently (as described below).
104 When first starting, the shell inspects argument 0, and
105 if it begins with a dash
107 the shell is also considered a login shell.
108 This is normally done automatically by the system
109 when the user first logs in.
110 A login shell first reads commands
116 If the environment variable
118 is set on entry to a shell, or is set in the
120 of a login shell, the shell then reads commands from the file named in
122 Therefore, a user should place commands that are to be executed only
125 file, and commands that are executed for every shell inside the
130 variable to some file by placing the following line in the file
132 in the home directory,
135 the filename desired:
137 .Dl ENV=$HOME/.shinit; export ENV
139 The first non-option argument specified on the command line
140 will be treated as the
141 name of a file from which to read commands (a shell script), and
142 the remaining arguments are set as the positional parameters
143 of the shell ($1, $2, etc).
144 Otherwise, the shell reads commands
145 from its standard input.
147 Unlike older versions of
151 script is only sourced on invocation of interactive shells.
153 closes a well-known, and sometimes easily exploitable security
154 hole related to poorly thought out
157 .Ss Argument List Processing
158 All of the single letter options to
160 have a corresponding long name,
161 with the exception of
165 These long names are provided next to the single letter options
166 in the descriptions below.
167 The long name for an option may be specified as an argument to the
171 Once the shell is running,
172 the long name for an option may be specified as an argument to the
177 (described later in the section called
178 .Sx Built-in Commands ) .
179 Introducing an option with a dash
189 will stop option processing and will force the remaining
190 words on the command line to be treated as arguments.
195 options do not have long names.
196 They take arguments and are described after the single letter options.
197 .Bl -tag -width indent
198 .It Fl a Li allexport
199 Flag variables for export when assignments are made to them.
201 Enable asynchronous notification of background job
204 .It Fl C Li noclobber
205 Do not overwrite existing files with
210 command line editor (disables the
212 option if it has been set).
214 Exit immediately if any untested command fails in non-interactive mode.
215 The exit status of a command is considered to be
216 explicitly tested if the command is part of the list used to control
218 .Ic if , elif , while ,
221 if the command is the left
226 operator; or if the command is a pipeline preceded by the
229 If a shell function is executed and its exit status is explicitly
230 tested, all commands of the function are considered to be tested as
233 Disable pathname expansion.
234 .It Fl I Li ignoreeof
237 from input when in interactive mode.
238 .It Fl i Li interactive
239 Force the shell to behave interactively.
241 Turn on job control (set automatically when interactive).
243 If not interactive, read commands but do not
245 This is useful for checking the
246 syntax of shell scripts.
248 Change the default for the
254 (logical directory layout)
257 (physical directory layout).
258 .It Fl p Li privileged
259 Turn on privileged mode.
260 This mode is enabled on startup
261 if either the effective user or group id is not equal to the
262 real user or group id.
263 Turning this mode off sets the
264 effective user and group ids to the real user and group ids.
265 When this mode is enabled for interactive shells, the file
266 .Pa /etc/suid_profile
267 is sourced instead of
271 is sourced, and the contents of the
273 variable are ignored.
275 Read commands from standard input (set automatically
276 if no file arguments are present).
278 no effect when set after the shell has already started
279 running (i.e., when set with the
282 .It Fl T Li trapsasync
283 When waiting for a child, execute traps immediately.
284 If this option is not set,
285 traps are executed after the child exits,
288 This nonstandard option is useful for putting guarding shells around
289 children that block signals.
290 The surrounding shell may kill the child
291 or it may just return control to the tty and leave the child alone,
293 .Bd -literal -offset indent
294 sh -T -c "trap 'exit 1' 2 ; some-blocking-program"
298 Write a message to standard error when attempting
299 to expand a variable that is not set, and if the
300 shell is not interactive, exit immediately.
304 command line editor (disables
308 The shell writes its input to standard error
310 Useful for debugging.
313 (preceded by the value of the
316 to standard error before it is executed.
317 Useful for debugging.
322 option causes the commands to be read from the
324 operand instead of from the standard input.
325 Keep in mind that this option only accepts a single string as its
326 argument, hence multi-word strings must be quoted.
330 option takes as its only argument the long name of an option
331 to be enabled or disabled.
332 For example, the following two invocations of
334 both enable the built-in
337 .Bd -literal -offset indent
342 If used without an argument, the
344 option displays the current option settings in a human-readable format.
347 is used without an argument, the current option settings are output
348 in a format suitable for re-input into the shell.
349 .Ss Lexical Structure
350 The shell reads input in terms of lines from a file and breaks
351 it up into words at whitespace (blanks and tabs), and at
355 which are special to the shell.
356 There are two types of operators: control operators and
357 redirection operators (their meaning is discussed later).
358 The following is a list of valid operators:
359 .Bl -tag -width indent
360 .It Control operators:
361 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
362 .It Li & Ta Li && Ta Li ( Ta Li ) Ta Li \en
363 .It Li ;; Ta Li ; Ta Li | Ta Li ||
365 .It Redirection operators:
366 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
367 .It Li < Ta Li > Ta Li << Ta Li >> Ta Li <>
368 .It Li <& Ta Li >& Ta Li <<- Ta Li >|
374 introduces a comment if used at the beginning of a word.
375 The word starting with
377 and the rest of the line are ignored.
379 Quoting is used to remove the special meaning of certain characters
380 or words to the shell, such as operators, whitespace, keywords,
383 There are three types of quoting: matched single quotes,
384 matched double quotes, and backslash.
385 .Bl -tag -width indent
387 Enclosing characters in single quotes preserves the literal
388 meaning of all the characters (except single quotes, making
389 it impossible to put single-quotes in a single-quoted string).
391 Enclosing characters within double quotes preserves the literal
392 meaning of all characters except dollarsign
398 The backslash inside double quotes is historically weird.
399 It remains literal unless it precedes the following characters,
400 which it serves to quote:
401 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
402 .It Li $ Ta Li ` Ta Li \&" Ta Li \e\ Ta Li \en
405 A backslash preserves the literal meaning of the following
406 character, with the exception of the newline character
408 A backslash preceding a newline is treated as a line continuation.
411 Reserved words are words that have special meaning to the
412 shell and are recognized at the beginning of a line and
413 after a control operator.
414 The following are reserved words:
415 .Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
416 .It Li \&! Ta { Ta } Ta Ic case Ta Ic do
417 .It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
418 .It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
421 An alias is a name and corresponding value set using the
424 Whenever a reserved word may occur (see above),
425 and after checking for reserved words, the shell
426 checks the word to see if it matches an alias.
427 If it does, it replaces it in the input stream with its value.
428 For example, if there is an alias called
433 .Bd -literal -offset indent
438 .Bd -literal -offset indent
442 Aliases provide a convenient way for naive users to
443 create shorthands for commands without having to learn how
444 to create functions with arguments.
446 used to create lexically obscure code.
447 This use is discouraged.
449 An alias name may be escaped in a command line, so that it is not
450 replaced by its alias value, by using quoting characters within or
451 adjacent to the alias name.
452 This is most often done by prefixing
453 an alias name with a backslash to execute a function, built-in, or
454 normal program with the same name.
459 The shell interprets the words it reads according to a
460 language, the specification of which is outside the scope
461 of this man page (refer to the BNF in the
464 Essentially though, a line is read and if
465 the first word of the line (or after a control operator)
466 is not a reserved word, then the shell has recognized a
468 Otherwise, a complex command or some
469 other special construct may have been recognized.
471 If a simple command has been recognized, the shell performs
472 the following actions:
475 Leading words of the form
477 are stripped off and assigned to the environment of
479 Redirection operators and
480 their arguments (as described below) are stripped
481 off and saved for processing.
483 The remaining words are expanded as described in
485 .Sx Word Expansions ,
486 and the first remaining word is considered the command
487 name and the command is located.
489 words are considered the arguments of the command.
490 If no command name resulted, then the
492 variable assignments recognized in 1) affect the
495 Redirections are performed as described in
499 Redirections are used to change where a command reads its input
501 In general, redirections open, close, or
502 duplicate an existing reference to a file.
504 used for redirection is:
506 .Dl [n] redir-op file
510 is one of the redirection operators mentioned
512 The following gives some examples of how these
513 operators can be used.
514 Note that stdin and stdout are commonly used abbreviations
515 for standard input and standard output respectively.
516 .Bl -tag -width "1234567890XX" -offset indent
518 redirect stdout (or file descriptor n) to file
520 same as above, but override the
524 append stdout (or file descriptor n) to file
526 redirect stdin (or file descriptor n) from file
528 redirect stdin (or file descriptor n) to and from file
530 duplicate stdin (or file descriptor n1) from file descriptor n2
532 close stdin (or file descriptor n)
534 duplicate stdout (or file descriptor n1) to file descriptor n2
536 close stdout (or file descriptor n)
539 The following redirection is often called a
541 .Bd -literal -offset indent
548 All the text on successive lines up to the delimiter is
549 saved away and made available to the command on standard
550 input, or file descriptor n if it is specified.
552 as specified on the initial line is quoted, then the here-doc-text
553 is treated literally, otherwise the text is subjected to
554 parameter expansion, command substitution, and arithmetic
555 expansion (as described in the section on
556 .Sx Word Expansions ) .
562 in the here-doc-text are stripped.
563 .Ss Search and Execution
564 There are three types of commands: shell functions,
565 built-in commands, and normal programs.
566 The command is searched for (by name) in that order.
567 The three types of commands are all executed in a different way.
569 When a shell function is executed, all of the shell positional
570 parameters (except $0, which remains unchanged) are
571 set to the arguments of the shell function.
572 The variables which are explicitly placed in the environment of
573 the command (by placing assignments to them before the
574 function name) are made local to the function and are set
576 Then the command given in the function definition is executed.
577 The positional parameters are restored to their original values
578 when the command completes.
579 This all occurs within the current shell.
581 Shell built-in commands are executed internally to the shell, without
582 spawning a new process.
584 Otherwise, if the command name does not match a function
585 or built-in command, the command is searched for as a normal
586 program in the file system (as described in the next section).
587 When a normal program is executed, the shell runs the program,
588 passing the arguments and the environment to the program.
589 If the program is not a normal executable file
590 (i.e., if it does not begin with the
600 the shell will interpret the program in a subshell.
601 The child shell will reinitialize itself in this case,
602 so that the effect will be
603 as if a new shell had been invoked to handle the ad-hoc shell script,
604 except that the location of hashed commands located in
605 the parent shell will be remembered by the child.
607 Note that previous versions of this document
608 and the source code itself misleadingly and sporadically
609 refer to a shell script without a magic number
611 .Qq shell procedure .
613 When locating a command, the shell first looks to see if
614 it has a shell function by that name.
616 built-in command by that name.
617 If a built-in command is not found,
618 one of two things happen:
621 Command names containing a slash are simply executed without
622 performing any searches.
624 The shell searches each entry in
626 in turn for the command.
629 variable should be a series of
630 entries separated by colons.
631 Each entry consists of a
633 The current directory
634 may be indicated implicitly by an empty directory name,
635 or explicitly by a single period.
637 .Ss Command Exit Status
638 Each command has an exit status that can influence the behavior
639 of other shell commands.
640 The paradigm is that a command exits
641 with zero for normal or success, and non-zero for failure,
642 error, or a false indication.
643 The man page for each command
644 should indicate the various exit codes and what they mean.
645 Additionally, the built-in commands return exit codes, as does
646 an executed shell function.
648 If a command is terminated by a signal, its exit status is 128 plus
650 Signal numbers are defined in the header file
653 Complex commands are combinations of simple commands
654 with control operators or reserved words, together creating a larger complex
656 More generally, a command is one of the following:
657 .Bl -item -offset indent
663 list or compound-list
670 Unless otherwise stated, the exit status of a command is
671 that of the last simple command executed by the command.
673 A pipeline is a sequence of one or more commands separated
674 by the control operator |.
675 The standard output of all but
676 the last command is connected to the standard input
678 The standard output of the last
679 command is inherited from the shell, as usual.
681 The format for a pipeline is:
683 .Dl [!] command1 [ | command2 ...]
685 The standard output of command1 is connected to the standard
687 The standard input, standard output, or
688 both of a command is considered to be assigned by the
689 pipeline before any redirection specified by redirection
690 operators that are part of the command.
692 If the pipeline is not in the background (discussed later),
693 the shell waits for all commands to complete.
697 does not precede the pipeline, the
698 exit status is the exit status of the last command specified
700 Otherwise, the exit status is the logical
701 NOT of the exit status of the last command.
703 the last command returns zero, the exit status is 1; if
704 the last command returns greater than zero, the exit status
707 Because pipeline assignment of standard input or standard
708 output or both takes place before redirection, it can be
709 modified by redirection.
712 .Dl $ command1 2>&1 | command2
714 sends both the standard output and standard error of
716 to the standard input of
721 or newline terminator causes the preceding
723 (described below in the section called
724 .Sx Short-Circuit List Operators )
725 to be executed sequentially;
728 causes asynchronous execution of the preceding AND-OR-list.
730 Note that unlike some other shells,
732 executes each process in the pipeline as a child of the
735 Shell built-in commands are the exception to this rule.
736 They are executed in the current shell, although they do not affect its
737 environment when used in pipelines.
738 .Ss Background Commands (&)
739 If a command is terminated by the control operator ampersand
741 the shell executes the command asynchronously;
742 the shell does not wait for the command to finish
743 before executing the next command.
745 The format for running a command in background is:
746 .Bd -literal -offset indent
747 command1 & [command2 & ...]
750 If the shell is not interactive, the standard input of an
751 asynchronous command is set to /dev/null.
752 .Ss Lists (Generally Speaking)
753 A list is a sequence of zero or more commands separated by
754 newlines, semicolons, or ampersands,
755 and optionally terminated by one of these three characters.
757 list are executed in the order they are written.
758 If command is followed by an ampersand, the shell starts the
759 command and immediately proceeds onto the next command;
760 otherwise it waits for the command to terminate before
761 proceeding to the next one.
762 .Ss Short-Circuit List Operators
766 are AND-OR list operators.
768 executes the first command, and then executes the second command
769 if the exit status of the first command is zero.
771 is similar, but executes the second command if the exit
772 status of the first command is nonzero.
776 both have the same priority.
777 .Ss Flow-Control Constructs (if, while, for, case)
782 .\" XXX Use .Dl to work around broken handling of .Ic inside .Bd and .Ed .
786 .Dl [ Ic elif Ar list
787 .Dl Ic then Ar list ] ...
788 .Dl [ Ic else Ar list ]
798 The two lists are executed repeatedly while the exit status of the
802 command is similar, but has the word
807 repeat until the exit status of the first list is zero.
812 .Dl Ic for Ar variable Op Ic in Ar word ...
818 and the following words are omitted,
821 The words are expanded, and then the list is executed
822 repeatedly with the variable set to each word in turn.
827 commands may be replaced with
837 .Dl Ic break Op Ar num
838 .Dl Ic continue Op Ar num
842 command terminates the
851 command continues with the next iteration of the innermost loop.
852 These are implemented as built-in commands.
857 .Dl Ic case Ar word Ic in
862 The pattern can actually be one or more patterns
869 .Ss Grouping Commands Together
870 Commands may be grouped by writing either
871 .Bd -literal -offset indent
876 .Bd -literal -offset indent
880 The first form executes the commands in a subshell.
881 Note that built-in commands thus executed do not affect the current shell.
882 The second form does not fork another shell,
883 so it is slightly more efficient.
884 Grouping commands together this way allows the user to
885 redirect their output as though they were one program:
886 .Bd -literal -offset indent
887 { echo -n "hello"; echo " world"; } > greeting
890 The syntax of a function definition is
891 .Bd -literal -offset indent
895 A function definition is an executable statement; when
896 executed it installs a function named name and returns an
898 The command is normally a list
904 Variables may be declared to be local to a function by
908 This should appear as the first statement of a function,
910 .Bd -ragged -offset indent
918 command is implemented as a built-in command.
920 When a variable is made local, it inherits the initial
921 value and exported and readonly flags from the variable
922 with the same name in the surrounding scope, if there is
924 Otherwise, the variable is initially unset.
926 uses dynamic scoping, so that if the variable
928 is made local to function
930 which then calls function
932 references to the variable
936 will refer to the variable
940 not to the global variable named
943 The only special parameter that can be made local is
947 local causes any shell options that are
948 changed via the set command inside the function to be
949 restored to their original values when the function
955 .Bd -ragged -offset indent
960 It terminates the current executional scope, returning from the previous
961 nested function, sourced script, or shell instance, in that order.
964 command is implemented as a built-in command.
965 .Ss Variables and Parameters
966 The shell maintains a set of parameters.
968 denoted by a name is called a variable.
970 the shell turns all the environment variables into shell
972 New variables can be set using the form
973 .Bd -literal -offset indent
977 Variables set by the user must have a name consisting solely
978 of alphabetics, numerics, and underscores.
979 The first letter of a variable name must not be numeric.
980 A parameter can also be denoted by a number
981 or a special character as explained below.
982 .Ss Positional Parameters
983 A positional parameter is a parameter denoted by a number greater than zero.
984 The shell sets these initially to the values of its command line
985 arguments that follow the name of the shell script.
988 built-in command can also be used to set or reset them.
989 .Ss Special Parameters
990 A special parameter is a parameter denoted by a special one-character
992 The special parameters recognized by the
996 are shown in the following list, exactly as they would appear in input
997 typed by the user or in the source of a shell script.
1000 Expands to the positional parameters, starting from one.
1002 the expansion occurs within a double-quoted string
1003 it expands to a single field with the value of each parameter
1004 separated by the first character of the
1013 Expands to the positional parameters, starting from one.
1015 the expansion occurs within double-quotes, each positional
1016 parameter expands as a separate argument.
1017 If there are no positional parameters, the
1020 generates zero arguments, even when
1023 What this basically means, for example, is
1032 .Bd -literal -offset indent
1036 Expands to the number of positional parameters.
1038 Expands to the exit status of the most recent pipeline.
1040 (hyphen) Expands to the current option flags (the single-letter
1041 option names concatenated into a string) as specified on
1042 invocation, by the set built-in command, or implicitly
1045 Expands to the process ID of the invoked shell.
1047 retains the same value of $ as its parent.
1049 Expands to the process ID of the most recent background
1050 command executed from the current shell.
1052 pipeline, the process ID is that of the last command in the
1055 (zero) Expands to the name of the shell or shell script.
1058 This clause describes the various expansions that are
1060 Not all expansions are performed on
1061 every word, as explained later.
1063 Tilde expansions, parameter expansions, command substitutions,
1064 arithmetic expansions, and quote removals that occur within
1065 a single word expand to a single field.
1067 splitting or pathname expansion that can create multiple
1068 fields from a single word.
1069 The single exception to this rule is
1070 the expansion of the special parameter
1072 within double-quotes,
1073 as was described above.
1075 The order of word expansion is:
1078 Tilde Expansion, Parameter Expansion, Command Substitution,
1079 Arithmetic Expansion (these all occur at the same time).
1081 Field Splitting is performed on fields generated by step (1)
1086 Pathname Expansion (unless the
1088 option is in effect).
1095 character is used to introduce parameter expansion, command
1096 substitution, or arithmetic evaluation.
1097 .Ss Tilde Expansion (substituting a user's home directory)
1098 A word beginning with an unquoted tilde character
1101 subjected to tilde expansion.
1102 All the characters up to a slash
1104 or the end of the word are treated as a username
1105 and are replaced with the user's home directory.
1107 username is missing (as in ~/foobar), the tilde is replaced
1108 with the value of the HOME variable (the current user's
1110 .Ss Parameter Expansion
1111 The format for parameter expansion is as follows:
1112 .Bd -literal -offset indent
1116 where expression consists of all characters until the matching
1120 escaped by a backslash or within a quoted string, and characters in
1121 embedded arithmetic expansions, command substitutions, and variable
1122 expansions, are not examined in determining the matching
1125 The simplest form for parameter expansion is:
1126 .Bd -literal -offset indent
1130 The value, if any, of parameter is substituted.
1132 The parameter name or symbol can be enclosed in braces, which are
1133 optional except for positional parameters with more than one digit or
1134 when parameter is followed by a character that could be interpreted as
1136 If a parameter expansion occurs inside double-quotes:
1139 Pathname expansion is not performed on the results of the
1142 Field splitting is not performed on the results of the
1143 expansion, with the exception of the special parameter
1147 In addition, a parameter expansion can be modified by using one of the
1149 .Bl -tag -width indent
1150 .It Li ${parameter:-word}
1152 If parameter is unset or
1153 null, the expansion of word is
1154 substituted; otherwise, the value of
1155 parameter is substituted.
1156 .It Li ${parameter:=word}
1157 Assign Default Values.
1158 If parameter is unset
1159 or null, the expansion of word is
1160 assigned to parameter.
1162 final value of parameter is
1164 Only variables, not positional
1165 parameters or special parameters, can be
1166 assigned in this way.
1167 .It Li ${parameter:?[word]}
1168 Indicate Error if Null or Unset.
1170 parameter is unset or null, the expansion of
1171 word (or a message indicating it is unset if
1172 word is omitted) is written to standard
1173 error and the shell exits with a nonzero
1175 Otherwise, the value of
1176 parameter is substituted.
1178 interactive shell need not exit.
1179 .It Li ${parameter:+word}
1180 Use Alternate Value.
1181 If parameter is unset
1182 or null, null is substituted;
1183 otherwise, the expansion of word is
1187 In the parameter expansions shown previously, use of the colon in the
1188 format results in a test for a parameter that is unset or null; omission
1189 of the colon results in a test for a parameter that is only unset.
1190 .Bl -tag -width indent
1191 .It Li ${#parameter}
1193 The length in characters of
1194 the value of parameter.
1197 The following four varieties of parameter expansion provide for substring
1199 In each case, pattern matching notation
1201 .Sx Shell Patterns ) ,
1202 rather than regular expression notation,
1203 is used to evaluate the patterns.
1204 If parameter is one of the special parameters
1208 the result of the expansion is unspecified.
1209 Enclosing the full parameter expansion string in double-quotes does not
1210 cause the following four varieties of pattern characters to be quoted,
1211 whereas quoting characters within the braces has this effect.
1212 .Bl -tag -width indent
1213 .It Li ${parameter%word}
1214 Remove Smallest Suffix Pattern.
1216 is expanded to produce a pattern.
1218 parameter expansion then results in
1219 parameter, with the smallest portion of the
1220 suffix matched by the pattern deleted.
1221 .It Li ${parameter%%word}
1222 Remove Largest Suffix Pattern.
1224 is expanded to produce a pattern.
1226 parameter expansion then results in
1227 parameter, with the largest portion of the
1228 suffix matched by the pattern deleted.
1229 .It Li ${parameter#word}
1230 Remove Smallest Prefix Pattern.
1232 is expanded to produce a pattern.
1234 parameter expansion then results in
1235 parameter, with the smallest portion of the
1236 prefix matched by the pattern deleted.
1237 .It Li ${parameter##word}
1238 Remove Largest Prefix Pattern.
1240 is expanded to produce a pattern.
1242 parameter expansion then results in
1243 parameter, with the largest portion of the
1244 prefix matched by the pattern deleted.
1246 .Ss Command Substitution
1247 Command substitution allows the output of a command to be substituted in
1248 place of the command name itself.
1249 Command substitution occurs when
1250 the command is enclosed as follows:
1251 .Bd -literal -offset indent
1255 or the backquoted version:
1256 .Bd -literal -offset indent
1260 The shell expands the command substitution by executing command in a
1261 subshell environment and replacing the command substitution
1262 with the standard output of the command,
1263 removing sequences of one or more newlines at the end of the substitution.
1264 Embedded newlines before the end of the output are not removed;
1265 however, during field splitting, they may be translated into spaces
1266 depending on the value of
1268 and the quoting that is in effect.
1269 .Ss Arithmetic Expansion
1270 Arithmetic expansion provides a mechanism for evaluating an arithmetic
1271 expression and substituting its value.
1272 The format for arithmetic expansion is as follows:
1273 .Bd -literal -offset indent
1277 The expression is treated as if it were in double-quotes, except
1278 that a double-quote inside the expression is not treated specially.
1280 shell expands all tokens in the expression for parameter expansion,
1281 command substitution, and quote removal.
1283 Next, the shell treats this as an arithmetic expression and
1284 substitutes the value of the expression.
1285 .Ss White Space Splitting (Field Splitting)
1286 After parameter expansion, command substitution, and
1287 arithmetic expansion the shell scans the results of
1288 expansions and substitutions that did not occur in double-quotes for
1289 field splitting and multiple fields can result.
1291 The shell treats each character of the
1293 as a delimiter and uses
1294 the delimiters to split the results of parameter expansion and command
1295 substitution into fields.
1296 .Ss Pathname Expansion (File Name Generation)
1300 file name generation is performed
1301 after word splitting is complete.
1303 viewed as a series of patterns, separated by slashes.
1305 process of expansion replaces the word with the names of
1306 all existing files whose names can be formed by replacing
1307 each pattern with a string that matches the specified pattern.
1308 There are two restrictions on this: first, a pattern cannot match
1309 a string containing a slash, and second,
1310 a pattern cannot match a string starting with a period
1311 unless the first character of the pattern is a period.
1312 The next section describes the patterns used for both
1313 Pathname Expansion and the
1317 A pattern consists of normal characters, which match themselves,
1318 and meta-characters.
1319 The meta-characters are
1325 These characters lose their special meanings if they are quoted.
1326 When command or variable substitution is performed and the dollar sign
1327 or back quotes are not double-quoted, the value of the
1328 variable or the output of the command is scanned for these
1329 characters and they are turned into meta-characters.
1333 matches any string of characters.
1336 matches any single character.
1339 introduces a character class.
1340 The end of the character class is indicated by a
1348 rather than introducing a character class.
1349 A character class matches any of the characters between the square brackets.
1350 A range of characters may be specified using a minus sign.
1351 The character class may be complemented by making an exclamation point
1355 the first character of the character class.
1359 in a character class, make it the first character listed
1367 make it the first or last character listed.
1368 .Ss Built-in Commands
1369 This section lists the commands which
1370 are built-in because they need to perform some operation
1371 that cannot be performed by a separate process.
1373 these, built-in versions of essential utilities
1374 are provided for efficiency.
1375 .Bl -tag -width indent
1377 A null command that returns a 0 (true) exit value.
1379 The commands in the specified file are read and executed by the shell.
1382 command may be used to return to the
1389 characters, it is used as is.
1390 Otherwise, the shell searches the
1393 If it is not found in the
1395 it is sought in the current working directory.
1397 A built-in equivalent of
1399 .It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc
1401 .Ar name Ns = Ns Ar string
1402 is specified, the shell defines the alias
1408 is specified, the value of the alias
1411 With no arguments, the
1413 built-in command prints the names and values of all defined aliases
1416 Alias values are written with appropriate quoting so that they are
1417 suitable for re-input to the shell.
1421 .It Ic bg Op Ar job ...
1422 Continue the specified jobs
1423 (or the current job if no jobs are given)
1425 .It Ic builtin Ar cmd Op Ar arg ...
1426 Execute the specified built-in command,
1428 This is useful when the user wishes to override a shell function
1429 with the same name as a built-in command.
1430 .It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
1431 List or alter key bindings for the line editor.
1432 This command is documented in
1434 .It Ic cd Oo Fl L | P Oc Op Ar directory
1435 Switch to the specified
1437 or to the directory specified in the
1439 environment variable if no
1448 then the directories listed in the
1451 searched for the specified
1455 is unset, the current directory is searched.
1458 is the same as that of
1460 In an interactive shell,
1463 command will print out the name of the directory
1464 that it actually switched to
1465 if this is different from the name that the user gave.
1466 These may be different either because the
1468 mechanism was used or because a symbolic link was crossed.
1472 option is specified,
1474 is handled physically and symbolic links are resolved before
1476 components are processed.
1479 option is specified,
1481 is handled logically.
1482 This is the default.
1487 .It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
1488 .It Ic command Oo Fl v | V Oc Op Ar utility
1489 The first form of invocation executes the specified
1491 as a simple command (see the
1497 option is specified, the command search is performed using a
1500 that is guaranteed to find all of the standard utilities.
1504 option is specified,
1506 is not executed but a description of its interpretation by the shell is
1508 For ordinary commands the output is the path name; for shell built-in
1509 commands, shell functions and keywords only the name is written.
1510 Aliases are printed as
1511 .Dq Ic alias Ar name Ns = Ns Ar value .
1515 option is identical to
1517 except for the output.
1519 .Dq Ar utility Ic is Ar description
1531 .It Ic echo Oo Fl e | n Oc Op Ar string ...
1532 Print a space-separated list of the arguments to the standard output
1533 and append a newline character.
1534 .Bl -tag -width indent
1536 Suppress the output of the trailing newline.
1538 Process C-style backslash escape sequences.
1540 understands the following character escapes:
1541 .Bl -tag -width indent
1543 Alert (ring the terminal bell)
1547 Suppress the trailing newline (this has the side-effect of truncating the
1548 line if it is not the last character)
1550 The ESC character (ASCII 0x1b)
1564 (Zero) The character whose octal value is nnn
1569 is not enclosed in quotes then the backslash itself must be escaped
1570 with a backslash to protect it from the shell.
1572 .Bd -literal -offset indent
1581 $ echo -e a\e\e\e\eb
1590 options may be specified.
1591 .It Ic eval Ar string ...
1592 Concatenate all the arguments with spaces.
1593 Then re-parse and execute the command.
1594 .It Ic exec Op Ar command Op arg ...
1598 the shell process is replaced with the specified program
1599 (which must be a real program, not a shell built-in command or function).
1600 Any redirections on the
1602 command are marked as permanent,
1603 so that they are not undone when the
1606 .It Ic exit Op Ar exitstatus
1607 Terminate the shell process.
1611 it is used as the exit status of the shell;
1612 otherwise the exit status of the preceding command is used.
1613 .It Ic export Ar name ...
1614 .It Ic export Op Fl p
1615 The specified names are exported so that they will
1616 appear in the environment of subsequent commands.
1617 The only way to un-export a variable is to
1620 The shell allows the value of a variable to be set
1621 at the same time as it is exported by writing
1622 .Bd -literal -offset indent
1626 With no arguments the export command lists the names
1627 of all exported variables.
1630 option is specified, the exported variables are printed as
1631 .Dq Ic export Ar name Ns = Ns Ar value
1632 lines, suitable for re-input to the shell.
1634 A null command that returns a non-zero (false) exit value.
1635 .It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
1636 .It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
1637 .It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
1640 built-in command lists, or edits and re-executes,
1641 commands previously entered to an interactive shell.
1642 .Bl -tag -width indent
1644 Use the editor named by
1646 to edit the commands.
1647 The editor string is a command name,
1648 subject to search via the
1653 variable is used as a default when
1658 is null or unset, the value of the
1665 is used as the editor.
1667 List the commands rather than invoking
1669 The commands are written in the
1670 sequence indicated by the first and last operands, as
1673 with each command preceded by the command number.
1675 Suppress command numbers when listing with
1678 Reverse the order of the commands listed
1687 Re-execute the command without invoking an editor.
1690 Select the commands to list or edit.
1691 The number of previous commands that can be accessed
1692 are determined by the value of the
1699 or both are one of the following:
1700 .Bl -tag -width indent
1702 A positive number representing a command number;
1703 command numbers can be displayed with the
1707 A negative decimal number representing the
1708 command that was executed
1711 commands previously.
1712 For example, -1 is the immediately previous command.
1714 A string indicating the most recently entered command
1715 that begins with that string.
1718 operand is not also specified with
1720 the string form of the first operand cannot contain an embedded equal sign.
1724 The following environment variables affect the execution of
1726 .Bl -tag -width ".Ev HISTSIZE"
1728 Name of the editor to use for history editing.
1730 The number of previous commands that are accessible.
1735 or the current job to the foreground.
1736 .It Ic getopts Ar optstring Ar var
1742 command deprecates the older
1745 The first argument should be a series of letters, each possibly
1746 followed by a colon which indicates that the option takes an argument.
1747 The specified variable is set to the parsed option.
1749 the next argument is placed into the shell variable
1751 If an option takes an argument, it is placed into the shell variable
1753 If an invalid option is encountered,
1757 It returns a false value (1) when it encounters the end of the options.
1758 .It Ic hash Oo Fl rv Oc Op Ar command ...
1759 The shell maintains a hash table which remembers the locations of commands.
1760 With no arguments whatsoever, the
1762 command prints out the contents of this table.
1763 Entries which have not been looked at since the last
1765 command are marked with an asterisk;
1766 it is possible for these entries to be invalid.
1770 command removes each specified
1772 from the hash table (unless they are functions) and then locates it.
1777 prints the locations of the commands as it finds them.
1782 command to delete all the entries in the hash table except for functions.
1783 .It Ic jobid Op Ar job
1784 Print the process id's of the processes in the specified
1788 argument is omitted, use the current job.
1789 .It Ic jobs Oo Fl lps Oc Op Ar job ...
1790 Print information about the specified jobs, or all jobs if no
1793 The information printed includes job ID, status and command name.
1797 option is specified, the PID of each job is also printed.
1800 option is specified, only the process IDs for the process group leaders
1801 are printed, one per line.
1804 option is specified, only the PIDs of the job commands are printed, one per
1806 .It Ic local Oo Ar variable ... Oc Op Fl
1810 .It Ic pwd Op Fl L | P
1811 Print the path of the current directory.
1812 The built-in command may
1813 differ from the program of the same name because the
1814 built-in command remembers what the current directory
1815 is rather than recomputing it each time.
1818 However, if the current directory is
1820 the built-in version of
1822 will continue to print the old name for the directory.
1826 option is specified, symbolic links are resolved.
1829 option is specified, the shell's notion of the current directory
1830 is printed (symbolic links are not resolved).
1831 This is the default.
1832 .It Ic read Oo Fl p Ar prompt Oc Oo Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
1838 and the standard input is a terminal.
1840 read from the standard input.
1841 The trailing newline
1842 is deleted from the line and the line is split as
1843 described in the section on
1844 .Sx White Space Splitting (Field Splitting)
1846 the pieces are assigned to the variables in order.
1847 If there are more pieces than variables, the remaining
1848 pieces (along with the characters in
1850 that separated them)
1851 are assigned to the last variable.
1852 If there are more variables than pieces, the remaining
1853 variables are assigned the null string.
1855 Backslashes are treated specially, unless the
1859 If a backslash is followed by
1860 a newline, the backslash and the newline will be
1862 If a backslash is followed by any other
1863 character, the backslash will be deleted and the following
1864 character will be treated as though it were not in
1870 option is specified and the
1872 elapses before any input is supplied,
1875 command will return an exit status of 1 without assigning any values.
1878 value may optionally be followed by one of
1883 to explicitly specify seconds, minutes or hours.
1884 If none is supplied,
1890 option exists only for backward compatibility with older scripts.
1891 .It Ic readonly Oo Fl p Oc Op Ar name ...
1894 is marked as read only,
1895 so that it cannot be subsequently modified or unset.
1896 The shell allows the value of a variable to be set
1897 at the same time as it is marked read only
1898 by using the following form:
1899 .Bd -literal -offset indent
1903 With no arguments the
1905 command lists the names of all read only variables.
1908 option is specified, the read-only variables are printed as
1909 .Dq Ic readonly Ar name Ns = Ns Ar value
1910 lines, suitable for re-input to the shell.
1911 .It Ic return Op Ar exitstatus
1915 .It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
1916 .Fl c Ar string Oc Op Fl - Ar arg ...
1919 command performs three different functions:
1922 With no arguments, it lists the values of all shell variables.
1924 If options are given,
1925 either in short form or using the long
1926 .Dq Fl /+o Ar longname
1928 it sets or clears the specified options as described in the section called
1929 .Sx Argument List Processing .
1933 option is specified,
1935 will replace the shell's positional parameters with the subsequent
1937 If no arguments follow the
1940 all the positional parameters will be cleared,
1941 which is equivalent to executing the command
1945 flag may be omitted when specifying arguments to be used
1946 as positional replacement parameters.
1947 This is not recommended,
1948 because the first argument may begin with a dash
1954 command will interpret as a request to enable or disable options.
1956 .It Ic setvar Ar variable Ar value
1957 Assigns the specified
1962 is intended to be used in functions that
1963 assign values to variables whose names are passed as parameters.
1964 In general it is better to write
1965 .Bd -literal -offset indent
1970 .It Ic shift Op Ar n
1971 Shift the positional parameters
1976 A shift sets the value of $1 to the value of $2,
1977 the value of $2 to the value of $3, and so on,
1978 decreasing the value of $# by one.
1979 If there are zero positional parameters, shifting does not do anything.
1981 A built-in equivalent of
1984 Print the amount of time spent executing the shell and its children.
1985 The first output line shows the user and system times for the shell
1986 itself, the second one contains the user and system times for the
1988 .It Ic trap Oo Ar action Oc Ar signal ...
1990 Cause the shell to parse and execute
1995 The signals are specified by name or number.
1996 In addition, the pseudo-signal
1998 may be used to specify an action that is performed when the shell terminates.
2001 may be an empty string or a dash
2003 the former causes the specified signal to be ignored
2004 and the latter causes the default action to be taken.
2007 is another way to request the default action, for compatibility reasons this
2008 usage is not recommended though.
2009 When the shell forks off a subshell,
2010 it resets trapped (but not ignored) signals to the default action.
2013 command has no effect on signals that were ignored on entry to the shell.
2019 command to display a list of valid signal names.
2021 A null command that returns a 0 (true) exit value.
2022 .It Ic type Op Ar name ...
2025 as a command and print the resolution of the command search.
2026 Possible resolutions are:
2027 shell keyword, alias, shell built-in command, command, tracked alias
2029 For aliases the alias expansion is printed;
2030 for commands and tracked aliases
2031 the complete pathname of the command is printed.
2032 .It Ic ulimit Oo Fl HSabcdflmnstuv Oc Op Ar limit
2033 Set or display resource limits (see
2037 is specified, the named resource will be set;
2038 otherwise the current resource value will be displayed.
2042 is specified, the hard limits will be set or displayed.
2043 While everybody is allowed to reduce a hard limit,
2044 only the superuser can increase it.
2048 specifies the soft limits instead.
2049 When displaying limits,
2055 The default is to display the soft limits,
2056 and to set both the hard and the soft limits.
2062 command to display all resources.
2065 is not acceptable in this mode.
2067 The remaining options specify which resource value is to be
2068 displayed or modified.
2069 They are mutually exclusive.
2070 .Bl -tag -width indent
2072 The maximum size of socket buffer usage, in bytes.
2073 .It Fl c Ar coredumpsize
2074 The maximal size of core dump files, in 512-byte blocks.
2075 .It Fl d Ar datasize
2076 The maximal size of the data segment of a process, in kilobytes.
2077 .It Fl f Ar filesize
2078 The maximal size of a file, in 512-byte blocks.
2079 .It Fl l Ar lockedmem
2080 The maximal size of memory that can be locked by a process, in
2082 .It Fl m Ar memoryuse
2083 The maximal resident set size of a process, in kilobytes.
2085 The maximal number of descriptors that could be opened by a process.
2086 .It Fl s Ar stacksize
2087 The maximal size of the stack segment, in kilobytes.
2089 The maximal amount of CPU time to be used by each process, in seconds.
2090 .It Fl u Ar userproc
2091 The maximal number of simultaneous processes for this user ID.
2092 .It Fl v Ar virtualmem
2093 The maximal virtual size of a process, in kilobytes.
2095 .It Ic umask Oo Fl S Oc Op Ar mask
2096 Set the file creation mask (see
2098 to the octal or symbolic (see
2102 If the argument is omitted, the current mask value is printed.
2105 option is specified, the output is symbolic, otherwise the output is octal.
2106 .It Ic unalias Oo Fl a Oc Op Ar name ...
2107 The specified alias names are removed.
2110 is specified, all aliases are removed.
2111 .It Ic unset Oo Fl fv Oc Ar name ...
2112 The specified variables or functions are unset and unexported.
2115 option is specified or no options are given, the
2117 arguments are treated as variable names.
2120 option is specified, the
2122 arguments are treated as function names.
2123 .It Ic wait Op Ar job
2124 Wait for the specified
2126 to complete and return the exit status of the last process in the
2128 If the argument is omitted, wait for all jobs to complete
2129 and return an exit status of zero.
2131 .Ss Commandline Editing
2134 is being used interactively from a terminal, the current command
2135 and the command history
2139 .Sx Built-in Commands )
2140 can be edited using vi-mode command line editing.
2141 This mode uses commands similar
2142 to a subset of those described in the vi man page.
2147 enables vi-mode editing and places
2149 into vi insert mode.
2150 With vi-mode enabled,
2152 can be switched between insert mode and command mode by typing
2156 while in command mode will pass the line to the shell.
2162 command can be used to enable a subset of
2163 emacs-style command line editing features.
2165 The following environment variables affect the execution of
2167 .Bl -tag -width ".Ev HISTSIZE"
2169 The search path used with the
2173 The fallback editor used with the
2176 If not set, the default editor is
2179 The default editor used with the
2183 The number of previous commands that are accessible.
2185 The starting directory of
2188 Input Field Separators.
2189 This is normally set to
2195 .Sx White Space Splitting
2196 section for more details.
2198 The name of a mail file, that will be checked for the arrival of new
2205 separated list of file names, for the shell to check for incoming
2207 This environment setting overrides the
2210 There is a maximum of 10 mailboxes that can be monitored at once.
2212 The default search path for executables.
2215 section for details.
2217 The primary prompt string, which defaults to
2219 unless you are the superuser, in which case it defaults to
2222 The secondary prompt string, which defaults to
2225 The prefix for the trace output (if
2231 The default terminal setting for the shell.
2232 This is inherited by children of the shell, and is used in the history
2236 Errors that are detected by the shell, such as a syntax error, will
2237 cause the shell to exit with a non-zero exit status.
2238 If the shell is not an interactive shell, the execution of the shell
2239 file will be aborted.
2240 Otherwise the shell will return the exit status of the last command
2241 executed, or if the exit builtin is used with a numeric argument, it
2242 will return the argument.
2262 command, the Thompson shell, appeared in
2264 It was superseded in
2266 by the Bourne shell, which inherited the name
2271 was rewritten in 1989 under the
2273 license after the Bourne shell from
2278 was originally written by
2279 .An Kenneth Almquist .
2283 utility does not recognize multibyte characters.