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 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
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 .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
40 .Nd command interpreter (shell)
43 .Op Fl /+abCEefIimnPpsTuVvx
44 .Op Fl /+o Ar longname
50 utility is the standard command interpreter for the system.
51 The current version of
53 is in the process of being changed to
56 specification for the shell.
57 This version has many features which make
59 similar in some respects to the Korn shell, but it is not a Korn
65 plus a few Berkeley extensions, are being
66 incorporated into this shell.
67 This man page is not intended to be a tutorial nor a complete
68 specification of the shell.
70 The shell is a command that reads lines from
71 either a file or the terminal, interprets them, and
72 generally executes other commands.
73 It is the program that is started when a user logs into the system,
74 although a user can select a different shell with the
78 implements a language that has flow control constructs,
79 a macro facility that provides a variety of features in
80 addition to data storage, along with built-in history and line
82 It incorporates many features to
83 aid interactive use and has the advantage that the interpretative
84 language is common to both interactive and non-interactive
86 That is, commands can be typed directly
87 to the running shell or can be put into a file,
88 which can be executed directly by the shell.
91 .\" XXX This next sentence is incredibly confusing.
93 If no arguments are present and if the standard input of the shell
94 is connected to a terminal
98 the shell is considered an interactive shell.
100 generally prompts before each command and handles programming
101 and command errors differently (as described below).
102 When first starting, the shell inspects argument 0, and
103 if it begins with a dash
105 the shell is also considered a login shell.
106 This is normally done automatically by the system
107 when the user first logs in.
108 A login shell first reads commands
113 in a user's home directory,
115 If the environment variable
117 is set on entry to a shell, or is set in the
119 of a login shell, the shell then reads commands from the file named in
121 Therefore, a user should place commands that are to be executed only
124 file, and commands that are executed for every shell inside the
129 variable to some file by placing the following line in the file
131 in the home directory,
134 the filename desired:
136 .Dl "ENV=$HOME/.shinit; export ENV"
138 The first non-option argument specified on the command line
139 will be treated as the
140 name of a file from which to read commands (a shell script), and
141 the remaining arguments are set as the positional parameters
145 Otherwise, the shell reads commands
146 from its standard input.
148 Unlike older versions of
152 script is only sourced on invocation of interactive shells.
154 closes a well-known, and sometimes easily exploitable security
155 hole related to poorly thought out
158 .Ss Argument List Processing
159 All of the single letter options to
161 have a corresponding long name,
162 with the exception of
166 These long names are provided next to the single letter options
167 in the descriptions below.
168 The long name for an option may be specified as an argument to the
172 Once the shell is running,
173 the long name for an option may be specified as an argument to the
178 (described later in the section called
179 .Sx Built-in Commands ) .
180 Introducing an option with a dash
190 will stop option processing and will force the remaining
191 words on the command line to be treated as arguments.
196 options do not have long names.
197 They take arguments and are described after the single letter options.
198 .Bl -tag -width indent
199 .It Fl a Li allexport
200 Flag variables for export when assignments are made to them.
202 Enable asynchronous notification of background job
205 .It Fl C Li noclobber
206 Do not overwrite existing files with
211 command line editor (disables the
213 option if it has been set).
215 Exit immediately if any untested command fails in non-interactive mode.
216 The exit status of a command is considered to be
217 explicitly tested if the command is part of the list used to control
219 .Ic if , elif , while ,
222 if the command is the left
227 operator; or if the command is a pipeline preceded by the
230 If a shell function is executed and its exit status is explicitly
231 tested, all commands of the function are considered to be tested as
234 Disable pathname expansion.
235 .It Fl I Li ignoreeof
238 from input when in interactive mode.
239 .It Fl i Li interactive
240 Force the shell to behave interactively.
242 Turn on job control (set automatically when interactive).
244 If not interactive, read commands but do not
246 This is useful for checking the
247 syntax of shell scripts.
249 Change the default for the
255 (logical directory layout)
258 (physical directory layout).
259 .It Fl p Li privileged
260 Turn on privileged mode.
261 This mode is enabled on startup
262 if either the effective user or group ID is not equal to the
263 real user or group ID.
264 Turning this mode off sets the
265 effective user and group IDs to the real user and group IDs.
266 When this mode is enabled for interactive shells, the file
267 .Pa /etc/suid_profile
268 is sourced instead of
272 is sourced, and the contents of the
274 variable are ignored.
276 Read commands from standard input (set automatically
277 if no file arguments are present).
279 no effect when set after the shell has already started
280 running (i.e., when set with the
283 .It Fl T Li trapsasync
284 When waiting for a child, execute traps immediately.
285 If this option is not set,
286 traps are executed after the child exits,
289 This nonstandard option is useful for putting guarding shells around
290 children that block signals.
291 The surrounding shell may kill the child
292 or it may just return control to the tty and leave the child alone,
294 .Bd -literal -offset indent
295 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.
381 characters (character code 0) are not allowed in shell input.
383 Quoting is used to remove the special meaning of certain characters
384 or words to the shell, such as operators, whitespace, keywords,
387 There are three types of quoting: matched single quotes,
388 matched double quotes, and backslash.
389 .Bl -tag -width indent
391 Enclosing characters in single quotes preserves the literal
392 meaning of all the characters (except single quotes, making
393 it impossible to put single-quotes in a single-quoted string).
395 Enclosing characters within double quotes preserves the literal
396 meaning of all characters except dollar sign
402 The backslash inside double quotes is historically weird.
403 It remains literal unless it precedes the following characters,
404 which it serves to quote:
405 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
406 .It Li $ Ta Li ` Ta Li \&" Ta Li \e\ Ta Li \en
409 A backslash preserves the literal meaning of the following
410 character, with the exception of the newline character
412 A backslash preceding a newline is treated as a line continuation.
415 Reserved words are words that have special meaning to the
416 shell and are recognized at the beginning of a line and
417 after a control operator.
418 The following are reserved words:
419 .Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
420 .It Li \&! Ta { Ta } Ta Ic case Ta Ic do
421 .It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
422 .It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
425 An alias is a name and corresponding value set using the
428 Whenever a reserved word may occur (see above),
429 and after checking for reserved words, the shell
430 checks the word to see if it matches an alias.
431 If it does, it replaces it in the input stream with its value.
432 For example, if there is an alias called
444 Aliases provide a convenient way for naive users to
445 create shorthands for commands without having to learn how
446 to create functions with arguments.
448 used to create lexically obscure code.
449 This use is discouraged.
451 An alias name may be escaped in a command line, so that it is not
452 replaced by its alias value, by using quoting characters within or
453 adjacent to the alias name.
454 This is most often done by prefixing
455 an alias name with a backslash to execute a function, built-in, or
456 normal program with the same name.
461 The shell interprets the words it reads according to a
462 language, the specification of which is outside the scope
463 of this man page (refer to the BNF in the
466 Essentially though, a line is read and if
467 the first word of the line (or after a control operator)
468 is not a reserved word, then the shell has recognized a
470 Otherwise, a complex command or some
471 other special construct may have been recognized.
473 If a simple command has been recognized, the shell performs
474 the following actions:
477 Leading words of the form
479 are stripped off and assigned to the environment of
481 Redirection operators and
482 their arguments (as described below) are stripped
483 off and saved for processing.
485 The remaining words are expanded as described in
487 .Sx Word Expansions ,
488 and the first remaining word is considered the command
489 name and the command is located.
491 words are considered the arguments of the command.
492 If no command name resulted, then the
494 variable assignments recognized in 1) affect the
497 Redirections are performed as described in
501 Redirections are used to change where a command reads its input
503 In general, redirections open, close, or
504 duplicate an existing reference to a file.
506 used for redirection is:
508 .D1 Oo Ar n Oc Ar redir-op file
512 is one of the redirection operators mentioned
514 The following gives some examples of how these
515 operators can be used.
516 Note that stdin and stdout are commonly used abbreviations
517 for standard input and standard output respectively.
518 .Bl -tag -width "1234567890XX" -offset indent
519 .It Oo Ar n Oc Ns Li > Ar file
520 redirect stdout (or file descriptor
524 .It Oo Ar n Oc Ns Li >| Ar file
525 same as above, but override the
528 .It Oo Ar n Oc Ns Li >> Ar file
529 append stdout (or file descriptor
533 .It Oo Ar n Oc Ns Li < Ar file
534 redirect stdin (or file descriptor
538 .It Oo Ar n Oc Ns Li <> Ar file
539 redirect stdin (or file descriptor
543 .It Oo Ar n1 Oc Ns Li <& Ns Ar n2
544 duplicate stdin (or file descriptor
548 .It Oo Ar n Oc Ns Li <&-
549 close stdin (or file descriptor
551 .It Oo Ar n1 Oc Ns Li >& Ns Ar n2
552 duplicate stdout (or file descriptor
556 .It Oo Ar n Oc Ns Li >&-
557 close stdout (or file descriptor
561 The following redirection is often called a
563 .Bd -unfilled -offset indent
564 .Oo Ar n Oc Ns Li << Ar delimiter
570 All the text on successive lines up to the delimiter is
571 saved away and made available to the command on standard
572 input, or file descriptor
577 as specified on the initial line is quoted, then the
579 is treated literally, otherwise the text is subjected to
580 parameter expansion, command substitution, and arithmetic
581 expansion (as described in the section on
582 .Sx Word Expansions ) .
591 .Ss Search and Execution
592 There are three types of commands: shell functions,
593 built-in commands, and normal programs.
594 The command is searched for (by name) in that order.
595 The three types of commands are all executed in a different way.
597 When a shell function is executed, all of the shell positional
600 which remains unchanged) are
601 set to the arguments of the shell function.
602 The variables which are explicitly placed in the environment of
603 the command (by placing assignments to them before the
604 function name) are made local to the function and are set
606 Then the command given in the function definition is executed.
607 The positional parameters are restored to their original values
608 when the command completes.
609 This all occurs within the current shell.
611 Shell built-in commands are executed internally to the shell, without
612 spawning a new process.
613 There are two kinds of built-in commands: regular and special.
614 Assignments before special builtins persist after they finish
615 executing and assignment errors, redirection errors and certain
616 operand errors cause a script to be aborted.
617 Both regular and special builtins can affect the shell in ways
618 normal programs cannot.
620 Otherwise, if the command name does not match a function
621 or built-in command, the command is searched for as a normal
622 program in the file system (as described in the next section).
623 When a normal program is executed, the shell runs the program,
624 passing the arguments and the environment to the program.
625 If the program is not a normal executable file
626 (i.e., if it does not begin with the
636 the shell will interpret the program in a subshell.
637 The child shell will reinitialize itself in this case,
638 so that the effect will be
639 as if a new shell had been invoked to handle the ad-hoc shell script,
640 except that the location of hashed commands located in
641 the parent shell will be remembered by the child
642 (see the description of the
644 built-in command below).
646 Note that previous versions of this document
647 and the source code itself misleadingly and sporadically
648 refer to a shell script without a magic number
650 .Dq "shell procedure" .
652 When locating a command, the shell first looks to see if
653 it has a shell function by that name.
655 built-in command by that name.
656 If a built-in command is not found,
657 one of two things happen:
660 Command names containing a slash are simply executed without
661 performing any searches.
663 The shell searches each entry in the
666 in turn for the command.
669 variable should be a series of
670 entries separated by colons.
671 Each entry consists of a
673 The current directory
674 may be indicated implicitly by an empty directory name,
675 or explicitly by a single period.
677 .Ss Command Exit Status
678 Each command has an exit status that can influence the behavior
679 of other shell commands.
680 The paradigm is that a command exits
681 with zero for normal or success, and non-zero for failure,
682 error, or a false indication.
683 The man page for each command
684 should indicate the various exit codes and what they mean.
685 Additionally, the built-in commands return exit codes, as does
686 an executed shell function.
688 If a command is terminated by a signal, its exit status is 128 plus
690 Signal numbers are defined in the header file
693 Complex commands are combinations of simple commands
694 with control operators or reserved words, together creating a larger complex
696 More generally, a command is one of the following:
697 .Bl -item -offset indent
703 list or compound-list
710 Unless otherwise stated, the exit status of a command is
711 that of the last simple command executed by the command.
713 A pipeline is a sequence of one or more commands separated
714 by the control operator
716 The standard output of all but
717 the last command is connected to the standard input
719 The standard output of the last
720 command is inherited from the shell, as usual.
722 The format for a pipeline is:
724 .D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ...
726 The standard output of
728 is connected to the standard input of
730 The standard input, standard output, or
731 both of a command is considered to be assigned by the
732 pipeline before any redirection specified by redirection
733 operators that are part of the command.
735 If the pipeline is not in the background (discussed later),
736 the shell waits for all commands to complete.
740 does not precede the pipeline, the
741 exit status is the exit status of the last command specified
743 Otherwise, the exit status is the logical
744 NOT of the exit status of the last command.
746 the last command returns zero, the exit status is 1; if
747 the last command returns greater than zero, the exit status
750 Because pipeline assignment of standard input or standard
751 output or both takes place before redirection, it can be
752 modified by redirection.
755 .Dl "command1 2>&1 | command2"
757 sends both the standard output and standard error of
759 to the standard input of
764 or newline terminator causes the preceding
766 (described below in the section called
767 .Sx Short-Circuit List Operators )
768 to be executed sequentially;
771 causes asynchronous execution of the preceding AND-OR-list.
773 Note that unlike some other shells,
775 executes each process in the pipeline as a child of the
778 Shell built-in commands are the exception to this rule.
779 They are executed in the current shell, although they do not affect its
780 environment when used in pipelines.
781 .Ss Background Commands (&)
782 If a command is terminated by the control operator ampersand
784 the shell executes the command asynchronously;
785 the shell does not wait for the command to finish
786 before executing the next command.
788 The format for running a command in background is:
790 .D1 Ar command1 Li & Op Ar command2 Li & Ar ...
792 If the shell is not interactive, the standard input of an
793 asynchronous command is set to
795 .Ss Lists (Generally Speaking)
796 A list is a sequence of zero or more commands separated by
797 newlines, semicolons, or ampersands,
798 and optionally terminated by one of these three characters.
800 list are executed in the order they are written.
801 If command is followed by an ampersand, the shell starts the
802 command and immediately proceeds onto the next command;
803 otherwise it waits for the command to terminate before
804 proceeding to the next one.
805 .Ss Short-Circuit List Operators
809 are AND-OR list operators.
811 executes the first command, and then executes the second command
812 if the exit status of the first command is zero.
814 is similar, but executes the second command if the exit
815 status of the first command is nonzero.
819 both have the same priority.
820 .Ss Flow-Control Constructs (if, while, for, case)
824 .Bd -unfilled -offset indent -compact
828 .Ic then Ar list Oc Ar ...
836 .Bd -unfilled -offset indent -compact
842 The two lists are executed repeatedly while the exit status of the
846 command is similar, but has the word
851 repeat until the exit status of the first list is zero.
856 .Bd -unfilled -offset indent -compact
857 .Ic for Ar variable Op Ic in Ar word ...
864 and the following words are omitted,
867 The words are expanded, and then the list is executed
868 repeatedly with the variable set to each word in turn.
873 commands may be replaced with
883 .D1 Ic break Op Ar num
884 .D1 Ic continue Op Ar num
888 command terminates the
897 command continues with the next iteration of the innermost loop.
898 These are implemented as special built-in commands.
903 .Bd -unfilled -offset indent -compact
904 .Ic case Ar word Ic in
905 .Ar pattern Ns Li ) Ar list Li ;;
910 The pattern can actually be one or more patterns
919 command is the exit code of the last command executed in the list or
920 zero if no patterns were matched.
921 .Ss Grouping Commands Together
922 Commands may be grouped by writing either
924 .D1 Li \&( Ns Ar list Ns Li \%)
928 .D1 Li { Ar list Ns Li \&; }
930 The first form executes the commands in a subshell.
931 Note that built-in commands thus executed do not affect the current shell.
932 The second form does not fork another shell,
933 so it is slightly more efficient.
934 Grouping commands together this way allows the user to
935 redirect their output as though they were one program:
936 .Bd -literal -offset indent
937 { echo -n "hello"; echo " world"; } > greeting
940 The syntax of a function definition is
942 .D1 Ar name Li \&( \&) Ar command
944 A function definition is an executable statement; when
945 executed it installs a function named
957 Variables may be declared to be local to a function by
961 This should appear as the first statement of a function,
964 .D1 Ic local Oo Ar variable ... Oc Op Fl
968 command is implemented as a built-in command.
970 When a variable is made local, it inherits the initial
971 value and exported and readonly flags from the variable
972 with the same name in the surrounding scope, if there is
974 Otherwise, the variable is initially unset.
976 uses dynamic scoping, so that if the variable
978 is made local to function
980 which then calls function
982 references to the variable
986 will refer to the variable
990 not to the global variable named
993 The only special parameter that can be made local is
997 local causes any shell options that are
1000 command inside the function to be
1001 restored to their original values when the function
1008 .D1 Ic return Op Ar exitstatus
1010 It terminates the current executional scope, returning from the previous
1011 nested function, sourced script, or shell instance, in that order.
1014 command is implemented as a special built-in command.
1015 .Ss Variables and Parameters
1016 The shell maintains a set of parameters.
1018 denoted by a name is called a variable.
1020 the shell turns all the environment variables into shell
1022 New variables can be set using the form
1024 .D1 Ar name Ns = Ns Ar value
1026 Variables set by the user must have a name consisting solely
1027 of alphabetics, numerics, and underscores.
1028 The first letter of a variable name must not be numeric.
1029 A parameter can also be denoted by a number
1030 or a special character as explained below.
1031 .Ss Positional Parameters
1032 A positional parameter is a parameter denoted by a number greater than zero.
1033 The shell sets these initially to the values of its command line
1034 arguments that follow the name of the shell script.
1037 built-in command can also be used to set or reset them.
1038 .Ss Special Parameters
1039 A special parameter is a parameter denoted by a special one-character
1041 The special parameters recognized by the
1045 are shown in the following list, exactly as they would appear in input
1046 typed by the user or in the source of a shell script.
1049 Expands to the positional parameters, starting from one.
1051 the expansion occurs within a double-quoted string
1052 it expands to a single field with the value of each parameter
1053 separated by the first character of the
1060 Expands to the positional parameters, starting from one.
1062 the expansion occurs within double-quotes, each positional
1063 parameter expands as a separate argument.
1064 If there are no positional parameters, the
1067 generates zero arguments, even when
1070 What this basically means, for example, is
1083 .Bd -literal -offset indent
1087 Expands to the number of positional parameters.
1089 Expands to the exit status of the most recent pipeline.
1091 (hyphen) Expands to the current option flags (the single-letter
1092 option names concatenated into a string) as specified on
1095 built-in command, or implicitly
1098 Expands to the process ID of the invoked shell.
1100 retains the same value of
1104 Expands to the process ID of the most recent background
1105 command executed from the current shell.
1107 pipeline, the process ID is that of the last command in the
1110 (zero) Expands to the name of the shell or shell script.
1113 This clause describes the various expansions that are
1115 Not all expansions are performed on
1116 every word, as explained later.
1118 Tilde expansions, parameter expansions, command substitutions,
1119 arithmetic expansions, and quote removals that occur within
1120 a single word expand to a single field.
1122 splitting or pathname expansion that can create multiple
1123 fields from a single word.
1124 The single exception to this rule is
1125 the expansion of the special parameter
1127 within double-quotes,
1128 as was described above.
1130 The order of word expansion is:
1133 Tilde Expansion, Parameter Expansion, Command Substitution,
1134 Arithmetic Expansion (these all occur at the same time).
1136 Field Splitting is performed on fields generated by step (1)
1141 Pathname Expansion (unless the
1143 option is in effect).
1150 character is used to introduce parameter expansion, command
1151 substitution, or arithmetic evaluation.
1152 .Ss Tilde Expansion (substituting a user's home directory)
1153 A word beginning with an unquoted tilde character
1156 subjected to tilde expansion.
1157 All the characters up to a slash
1159 or the end of the word are treated as a username
1160 and are replaced with the user's home directory.
1162 username is missing (as in
1164 the tilde is replaced with the value of the
1166 variable (the current user's home directory).
1167 .Ss Parameter Expansion
1168 The format for parameter expansion is as follows:
1170 .D1 Li ${ Ns Ar expression Ns Li }
1174 consists of all characters until the matching
1178 escaped by a backslash or within a quoted string, and characters in
1179 embedded arithmetic expansions, command substitutions, and variable
1180 expansions, are not examined in determining the matching
1183 The simplest form for parameter expansion is:
1185 .D1 Li ${ Ns Ar parameter Ns Li }
1187 The value, if any, of
1191 The parameter name or symbol can be enclosed in braces, which are
1192 optional except for positional parameters with more than one digit or
1193 when parameter is followed by a character that could be interpreted as
1195 If a parameter expansion occurs inside double-quotes:
1198 Pathname expansion is not performed on the results of the
1201 Field splitting is not performed on the results of the
1202 expansion, with the exception of the special parameter
1206 In addition, a parameter expansion can be modified by using one of the
1208 .Bl -tag -width indent
1209 .It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li }
1213 is unset or null, the expansion of
1215 is substituted; otherwise, the value of
1218 .It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li }
1219 Assign Default Values.
1222 is unset or null, the expansion of
1232 does not prevent field splitting or pathname expansion.
1233 Only variables, not positional
1234 parameters or special parameters, can be
1235 assigned in this way.
1236 .It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li }
1237 Indicate Error if Null or Unset.
1240 is unset or null, the expansion of
1242 (or a message indicating it is unset if
1244 is omitted) is written to standard
1245 error and the shell exits with a nonzero
1247 Otherwise, the value of
1251 interactive shell need not exit.
1252 .It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li }
1253 Use Alternate Value.
1256 is unset or null, null is substituted;
1257 otherwise, the expansion of
1262 In the parameter expansions shown previously, use of the colon in the
1263 format results in a test for a parameter that is unset or null; omission
1264 of the colon results in a test for a parameter that is only unset.
1265 .Bl -tag -width indent
1266 .It Li ${# Ns Ar parameter Ns Li }
1268 The length in characters of
1273 The following four varieties of parameter expansion provide for substring
1275 In each case, pattern matching notation
1277 .Sx Shell Patterns ) ,
1278 rather than regular expression notation,
1279 is used to evaluate the patterns.
1280 If parameter is one of the special parameters
1284 the result of the expansion is unspecified.
1285 Enclosing the full parameter expansion string in double-quotes does not
1286 cause the following four varieties of pattern characters to be quoted,
1287 whereas quoting characters within the braces has this effect.
1288 .Bl -tag -width indent
1289 .It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li }
1290 Remove Smallest Suffix Pattern.
1293 is expanded to produce a pattern.
1295 parameter expansion then results in
1297 with the smallest portion of the
1298 suffix matched by the pattern deleted.
1299 .It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li }
1300 Remove Largest Suffix Pattern.
1303 is expanded to produce a pattern.
1305 parameter expansion then results in
1307 with the largest portion of the
1308 suffix matched by the pattern deleted.
1309 .It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li }
1310 Remove Smallest Prefix Pattern.
1313 is expanded to produce a pattern.
1315 parameter expansion then results in
1317 with the smallest portion of the
1318 prefix matched by the pattern deleted.
1319 .It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li }
1320 Remove Largest Prefix Pattern.
1323 is expanded to produce a pattern.
1325 parameter expansion then results in
1327 with the largest portion of the
1328 prefix matched by the pattern deleted.
1330 .Ss Command Substitution
1331 Command substitution allows the output of a command to be substituted in
1332 place of the command name itself.
1333 Command substitution occurs when
1334 the command is enclosed as follows:
1336 .D1 Li $( Ns Ar command Ns Li )\&
1338 or the backquoted version:
1340 .D1 Li ` Ns Ar command Ns Li `
1342 The shell expands the command substitution by executing command in a
1343 subshell environment and replacing the command substitution
1344 with the standard output of the command,
1345 removing sequences of one or more newlines at the end of the substitution.
1346 Embedded newlines before the end of the output are not removed;
1347 however, during field splitting, they may be translated into spaces
1348 depending on the value of
1350 and the quoting that is in effect.
1351 .Ss Arithmetic Expansion
1352 Arithmetic expansion provides a mechanism for evaluating an arithmetic
1353 expression and substituting its value.
1354 The format for arithmetic expansion is as follows:
1356 .D1 Li $(( Ns Ar expression Ns Li ))
1360 is treated as if it were in double-quotes, except
1361 that a double-quote inside the expression is not treated specially.
1363 shell expands all tokens in the
1365 for parameter expansion,
1366 command substitution, and quote removal.
1368 Next, the shell treats this as an arithmetic expression and
1369 substitutes the value of the expression.
1370 .Ss White Space Splitting (Field Splitting)
1371 After parameter expansion, command substitution, and
1372 arithmetic expansion the shell scans the results of
1373 expansions and substitutions that did not occur in double-quotes for
1374 field splitting and multiple fields can result.
1376 The shell treats each character of the
1378 variable as a delimiter and uses
1379 the delimiters to split the results of parameter expansion and command
1380 substitution into fields.
1381 .Ss Pathname Expansion (File Name Generation)
1385 file name generation is performed
1386 after word splitting is complete.
1388 viewed as a series of patterns, separated by slashes.
1390 process of expansion replaces the word with the names of
1391 all existing files whose names can be formed by replacing
1392 each pattern with a string that matches the specified pattern.
1393 There are two restrictions on this: first, a pattern cannot match
1394 a string containing a slash, and second,
1395 a pattern cannot match a string starting with a period
1396 unless the first character of the pattern is a period.
1397 The next section describes the patterns used for both
1398 Pathname Expansion and the
1402 A pattern consists of normal characters, which match themselves,
1403 and meta-characters.
1404 The meta-characters are
1410 These characters lose their special meanings if they are quoted.
1411 When command or variable substitution is performed and the dollar sign
1412 or back quotes are not double-quoted, the value of the
1413 variable or the output of the command is scanned for these
1414 characters and they are turned into meta-characters.
1418 matches any string of characters.
1421 matches any single character.
1424 introduces a character class.
1425 The end of the character class is indicated by a
1433 rather than introducing a character class.
1434 A character class matches any of the characters between the square brackets.
1435 A range of characters may be specified using a minus sign.
1436 The character class may be complemented by making an exclamation point
1438 the first character of the character class.
1442 in a character class, make it the first character listed
1448 make it the first or last character listed.
1449 .Ss Built-in Commands
1450 This section lists the commands which
1451 are built-in because they need to perform some operation
1452 that cannot be performed by a separate process.
1454 these, built-in versions of essential utilities
1455 are provided for efficiency.
1456 .Bl -tag -width indent
1458 A null command that returns a 0 (true) exit value.
1460 The commands in the specified file are read and executed by the shell.
1463 command may be used to return to the
1470 characters, it is used as is.
1471 Otherwise, the shell searches the
1474 If it is not found in the
1476 it is sought in the current working directory.
1478 A built-in equivalent of
1480 .It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc
1482 .Ar name Ns = Ns Ar string
1483 is specified, the shell defines the alias
1489 is specified, the value of the alias
1492 With no arguments, the
1494 built-in command prints the names and values of all defined aliases
1497 Alias values are written with appropriate quoting so that they are
1498 suitable for re-input to the shell.
1502 .It Ic bg Op Ar job ...
1503 Continue the specified jobs
1504 (or the current job if no jobs are given)
1506 .It Ic builtin Ar cmd Op Ar arg ...
1507 Execute the specified built-in command,
1509 This is useful when the user wishes to override a shell function
1510 with the same name as a built-in command.
1511 .It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
1512 List or alter key bindings for the line editor.
1513 This command is documented in
1515 .It Ic cd Oo Fl L | P Oc Op Ar directory
1516 Switch to the specified
1518 or to the directory specified in the
1520 environment variable if no
1529 then the directories listed in the
1532 searched for the specified
1536 is unset, the current directory is searched.
1539 is the same as that of
1541 In an interactive shell,
1544 command will print out the name of the directory
1545 that it actually switched to
1546 if this is different from the name that the user gave.
1547 These may be different either because the
1549 mechanism was used or because a symbolic link was crossed.
1553 option is specified,
1555 is handled physically and symbolic links are resolved before
1557 components are processed.
1560 option is specified,
1562 is handled logically.
1563 This is the default.
1568 .It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
1569 .It Ic command Oo Fl v | V Oc Op Ar utility
1570 The first form of invocation executes the specified
1572 as a simple command (see the
1578 option is specified, the command search is performed using a
1581 that is guaranteed to find all of the standard utilities.
1585 option is specified,
1587 is not executed but a description of its interpretation by the shell is
1589 For ordinary commands the output is the path name; for shell built-in
1590 commands, shell functions and keywords only the name is written.
1591 Aliases are printed as
1592 .Dq Ic alias Ar name Ns = Ns Ar value .
1596 option is identical to
1598 except for the output.
1600 .Dq Ar utility Ic is Ar description
1606 a special shell builtin,
1613 .It Ic echo Oo Fl e | n Oc Op Ar string ...
1614 Print a space-separated list of the arguments to the standard output
1615 and append a newline character.
1616 .Bl -tag -width indent
1618 Suppress the output of the trailing newline.
1620 Process C-style backslash escape sequences.
1623 command understands the following character escapes:
1624 .Bl -tag -width indent
1626 Alert (ring the terminal bell)
1630 Suppress the trailing newline (this has the side-effect of truncating the
1631 line if it is not the last character)
1649 (Zero) The character whose octal value is
1655 is not enclosed in quotes then the backslash itself must be escaped
1656 with a backslash to protect it from the shell.
1658 .Bd -literal -offset indent
1667 $ echo -e a\e\e\e\eb
1676 options may be specified.
1677 .It Ic eval Ar string ...
1678 Concatenate all the arguments with spaces.
1679 Then re-parse and execute the command.
1680 .It Ic exec Op Ar command Op arg ...
1684 the shell process is replaced with the specified program
1685 (which must be a real program, not a shell built-in command or function).
1686 Any redirections on the
1688 command are marked as permanent,
1689 so that they are not undone when the
1692 .It Ic exit Op Ar exitstatus
1693 Terminate the shell process.
1697 it is used as the exit status of the shell;
1698 otherwise the exit status of the preceding command is used.
1699 The exit status should be an integer between 0 and 255.
1700 .It Ic export Ar name ...
1701 .It Ic export Op Fl p
1702 The specified names are exported so that they will
1703 appear in the environment of subsequent commands.
1704 The only way to un-export a variable is to
1707 The shell allows the value of a variable to be set
1708 at the same time as it is exported by writing
1710 .D1 Ic export Ar name Ns = Ns Ar value
1712 With no arguments the
1714 command lists the names
1715 of all exported variables.
1718 option is specified, the exported variables are printed as
1719 .Dq Ic export Ar name Ns = Ns Ar value
1720 lines, suitable for re-input to the shell.
1722 A null command that returns a non-zero (false) exit value.
1723 .It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
1724 .It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
1725 .It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
1728 built-in command lists, or edits and re-executes,
1729 commands previously entered to an interactive shell.
1730 .Bl -tag -width indent
1732 Use the editor named by
1734 to edit the commands.
1737 string is a command name,
1738 subject to search via the
1743 variable is used as a default when
1748 is null or unset, the value of the
1755 is used as the editor.
1757 List the commands rather than invoking
1759 The commands are written in the
1760 sequence indicated by the
1764 operands, as affected by
1766 with each command preceded by the command number.
1768 Suppress command numbers when listing with
1771 Reverse the order of the commands listed
1780 Re-execute the command without invoking an editor.
1783 Select the commands to list or edit.
1784 The number of previous commands that can be accessed
1785 are determined by the value of the
1792 or both are one of the following:
1793 .Bl -tag -width indent
1794 .It Oo Cm + Oc Ns Ar num
1795 A positive number representing a command number;
1796 command numbers can be displayed with the
1800 A negative decimal number representing the
1801 command that was executed
1804 commands previously.
1805 For example, \-1 is the immediately previous command.
1807 A string indicating the most recently entered command
1808 that begins with that string.
1810 .Ar old Ns = Ns Ar new
1811 operand is not also specified with
1813 the string form of the first operand cannot contain an embedded equal sign.
1817 The following environment variables affect the execution of
1819 .Bl -tag -width ".Ev HISTSIZE"
1821 Name of the editor to use for history editing.
1823 The number of previous commands that are accessible.
1828 or the current job to the foreground.
1829 .It Ic getopts Ar optstring var
1836 command deprecates the older
1839 The first argument should be a series of letters, each possibly
1840 followed by a colon which indicates that the option takes an argument.
1841 The specified variable is set to the parsed option.
1843 the next argument is placed into the shell variable
1845 If an option takes an argument, it is placed into the shell variable
1847 If an invalid option is encountered,
1851 It returns a false value (1) when it encounters the end of the options.
1852 .It Ic hash Oo Fl rv Oc Op Ar command ...
1853 The shell maintains a hash table which remembers the locations of commands.
1854 With no arguments whatsoever, the
1856 command prints out the contents of this table.
1857 Entries which have not been looked at since the last
1859 command are marked with an asterisk;
1860 it is possible for these entries to be invalid.
1864 command removes each specified
1866 from the hash table (unless they are functions) and then locates it.
1871 prints the locations of the commands as it finds them.
1876 command to delete all the entries in the hash table except for functions.
1877 .It Ic jobid Op Ar job
1878 Print the process IDs of the processes in the specified
1882 argument is omitted, use the current job.
1883 .It Ic jobs Oo Fl lps Oc Op Ar job ...
1884 Print information about the specified jobs, or all jobs if no
1887 The information printed includes job ID, status and command name.
1891 option is specified, the PID of each job is also printed.
1894 option is specified, only the process IDs for the process group leaders
1895 are printed, one per line.
1898 option is specified, only the PIDs of the job commands are printed, one per
1900 .It Ic local Oo Ar variable ... Oc Op Fl
1904 .It Ic pwd Op Fl L | P
1905 Print the path of the current directory.
1906 The built-in command may
1907 differ from the program of the same name because the
1908 built-in command remembers what the current directory
1909 is rather than recomputing it each time.
1912 However, if the current directory is
1914 the built-in version of
1916 will continue to print the old name for the directory.
1920 option is specified, symbolic links are resolved.
1923 option is specified, the shell's notion of the current directory
1924 is printed (symbolic links are not resolved).
1925 This is the default.
1926 .It Ic read Oo Fl p Ar prompt Oc Oo
1927 .Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
1933 and the standard input is a terminal.
1935 read from the standard input.
1936 The trailing newline
1937 is deleted from the line and the line is split as
1938 described in the section on
1939 .Sx White Space Splitting (Field Splitting)
1941 the pieces are assigned to the variables in order.
1942 If there are more pieces than variables, the remaining
1943 pieces (along with the characters in
1945 that separated them)
1946 are assigned to the last variable.
1947 If there are more variables than pieces, the remaining
1948 variables are assigned the null string.
1950 Backslashes are treated specially, unless the
1954 If a backslash is followed by
1955 a newline, the backslash and the newline will be
1957 If a backslash is followed by any other
1958 character, the backslash will be deleted and the following
1959 character will be treated as though it were not in
1965 option is specified and the
1967 elapses before a complete line of input is supplied,
1970 command will return an exit status of 1 without assigning any values.
1973 value may optionally be followed by one of
1978 to explicitly specify seconds, minutes or hours.
1979 If none is supplied,
1985 option exists only for backward compatibility with older scripts.
1986 .It Ic readonly Oo Fl p Oc Op Ar name ...
1989 is marked as read only,
1990 so that it cannot be subsequently modified or unset.
1991 The shell allows the value of a variable to be set
1992 at the same time as it is marked read only
1993 by using the following form:
1995 .D1 Ic readonly Ar name Ns = Ns Ar value
1997 With no arguments the
1999 command lists the names of all read only variables.
2002 option is specified, the read-only variables are printed as
2003 .Dq Ic readonly Ar name Ns = Ns Ar value
2004 lines, suitable for re-input to the shell.
2005 .It Ic return Op Ar exitstatus
2009 .It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
2010 .Fl c Ar string Oc Op Fl - Ar arg ...
2013 command performs three different functions:
2016 With no arguments, it lists the values of all shell variables.
2018 If options are given,
2019 either in short form or using the long
2020 .Dq Fl /+o Ar longname
2022 it sets or clears the specified options as described in the section called
2023 .Sx Argument List Processing .
2027 option is specified,
2029 will replace the shell's positional parameters with the subsequent
2031 If no arguments follow the
2034 all the positional parameters will be cleared,
2035 which is equivalent to executing the command
2039 flag may be omitted when specifying arguments to be used
2040 as positional replacement parameters.
2041 This is not recommended,
2042 because the first argument may begin with a dash
2048 command will interpret as a request to enable or disable options.
2050 .It Ic setvar Ar variable value
2051 Assigns the specified
2057 command is intended to be used in functions that
2058 assign values to variables whose names are passed as parameters.
2059 In general it is better to write
2060 .Dq Ar variable Ns = Ns Ar value
2063 .It Ic shift Op Ar n
2064 Shift the positional parameters
2069 A shift sets the value of
2078 decreasing the value of
2081 If there are zero positional parameters, shifting does not do anything.
2083 A built-in equivalent of
2086 Print the amount of time spent executing the shell and its children.
2087 The first output line shows the user and system times for the shell
2088 itself, the second one contains the user and system times for the
2090 .It Ic trap Oo Ar action Oc Ar signal ...
2092 Cause the shell to parse and execute
2097 The signals are specified by name or number.
2098 In addition, the pseudo-signal
2100 may be used to specify an
2102 that is performed when the shell terminates.
2105 may be an empty string or a dash
2107 the former causes the specified signal to be ignored
2108 and the latter causes the default action to be taken.
2111 is another way to request the default action, for compatibility reasons this
2112 usage is not recommended though.
2113 When the shell forks off a subshell,
2114 it resets trapped (but not ignored) signals to the default action.
2117 command has no effect on signals that were ignored on entry to the shell.
2123 command to display a list of valid signal names.
2125 A null command that returns a 0 (true) exit value.
2126 .It Ic type Op Ar name ...
2129 as a command and print the resolution of the command search.
2130 Possible resolutions are:
2131 shell keyword, alias, special shell builtin, shell builtin, command,
2134 For aliases the alias expansion is printed;
2135 for commands and tracked aliases
2136 the complete pathname of the command is printed.
2137 .It Ic ulimit Oo Fl HSabcdflmnpstuvw Oc Op Ar limit
2138 Set or display resource limits (see
2142 is specified, the named resource will be set;
2143 otherwise the current resource value will be displayed.
2147 is specified, the hard limits will be set or displayed.
2148 While everybody is allowed to reduce a hard limit,
2149 only the superuser can increase it.
2153 specifies the soft limits instead.
2154 When displaying limits,
2160 The default is to display the soft limits,
2161 and to set both the hard and the soft limits.
2167 command to display all resources.
2170 is not acceptable in this mode.
2172 The remaining options specify which resource value is to be
2173 displayed or modified.
2174 They are mutually exclusive.
2175 .Bl -tag -width indent
2177 The maximum size of socket buffer usage, in bytes.
2178 .It Fl c Ar coredumpsize
2179 The maximal size of core dump files, in 512-byte blocks.
2180 .It Fl d Ar datasize
2181 The maximal size of the data segment of a process, in kilobytes.
2182 .It Fl f Ar filesize
2183 The maximal size of a file, in 512-byte blocks.
2184 .It Fl l Ar lockedmem
2185 The maximal size of memory that can be locked by a process, in
2187 .It Fl m Ar memoryuse
2188 The maximal resident set size of a process, in kilobytes.
2190 The maximal number of descriptors that could be opened by a process.
2191 .It Fl p Ar pseudoterminals
2192 The maximal number of pseudo-terminals for this user ID.
2193 .It Fl s Ar stacksize
2194 The maximal size of the stack segment, in kilobytes.
2196 The maximal amount of CPU time to be used by each process, in seconds.
2197 .It Fl u Ar userproc
2198 The maximal number of simultaneous processes for this user ID.
2199 .It Fl v Ar virtualmem
2200 The maximal virtual size of a process, in kilobytes.
2202 The maximum amount of swap space reserved or used for this user ID,
2205 .It Ic umask Oo Fl S Oc Op Ar mask
2206 Set the file creation mask (see
2208 to the octal or symbolic (see
2212 If the argument is omitted, the current mask value is printed.
2215 option is specified, the output is symbolic, otherwise the output is octal.
2216 .It Ic unalias Oo Fl a Oc Op Ar name ...
2217 The specified alias names are removed.
2220 is specified, all aliases are removed.
2221 .It Ic unset Oo Fl fv Oc Ar name ...
2222 The specified variables or functions are unset and unexported.
2225 option is specified or no options are given, the
2227 arguments are treated as variable names.
2230 option is specified, the
2232 arguments are treated as function names.
2233 .It Ic wait Op Ar job
2234 Wait for the specified
2236 to complete and return the exit status of the last process in the
2238 If the argument is omitted, wait for all jobs to complete
2239 and return an exit status of zero.
2241 .Ss Commandline Editing
2244 is being used interactively from a terminal, the current command
2245 and the command history
2249 .Sx Built-in Commands )
2252 command line editing.
2253 This mode uses commands similar
2254 to a subset of those described in the
2272 can be switched between insert mode and command mode by typing
2276 while in command mode will pass the line to the shell.
2279 .Dq Li "set -o emacs"
2282 command can be used to enable a subset of
2284 command line editing features.
2286 The following environment variables affect the execution of
2288 .Bl -tag -width ".Ev HISTSIZE"
2290 The search path used with the
2294 The fallback editor used with the
2297 If not set, the default editor is
2300 The default editor used with the
2304 The number of previous commands that are accessible.
2306 The starting directory of
2309 Input Field Separators.
2310 This is normally set to
2316 .Sx White Space Splitting
2317 section for more details.
2319 The name of a mail file, that will be checked for the arrival of new
2326 separated list of file names, for the shell to check for incoming
2328 This environment setting overrides the
2331 There is a maximum of 10 mailboxes that can be monitored at once.
2333 The default search path for executables.
2336 section for details.
2338 The primary prompt string, which defaults to
2340 unless you are the superuser, in which case it defaults to
2343 The secondary prompt string, which defaults to
2346 The prefix for the trace output (if
2352 The default terminal setting for the shell.
2353 This is inherited by children of the shell, and is used in the history
2357 Errors that are detected by the shell, such as a syntax error, will
2358 cause the shell to exit with a non-zero exit status.
2359 If the shell is not an interactive shell, the execution of the shell
2360 file will be aborted.
2361 Otherwise the shell will return the exit status of the last command
2364 builtin is used with a numeric argument, it
2365 will return the argument.
2384 command, the Thompson shell, appeared in
2386 It was superseded in
2388 by the Bourne shell, which inherited the name
2393 was rewritten in 1989 under the
2395 license after the Bourne shell from
2400 was originally written by
2401 .An Kenneth Almquist .
2405 utility does not recognize multibyte characters.