1 .\" Copyright (c) 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" This product includes software developed by the University of
18 .\" California, Berkeley and its contributors.
19 .\" 4. Neither the name of the University nor the names of its contributors
20 .\" may be used to endorse or promote products derived from this software
21 .\" without specific prior written permission.
23 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
36 .\" $FreeBSD: src/bin/sh/sh.1,v 1.39.2.25 2003/02/13 19:28:08 fanf Exp $
43 .Nd command interpreter (shell)
46 .Op Fl /+abCEefIimnPpsTuVvx
47 .Op Fl /+o Ar longname
53 utility is the standard command interpreter for the system.
54 The current version of
56 is in the process of being changed to
59 specification for the shell. This version has many features which make
61 similar in some respects to the Korn shell, but it is not a Korn
65 designated by POSIX, 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
81 editing capabilities. It incorporates many features to
82 aid interactive use and has the advantage that the interpretative
83 language is common to both interactive and non-interactive
84 use (shell scripts). That is, commands can be typed directly
85 to the running shell or can be put into a file,
86 which can be executed directly by the shell.
89 .\" XXX This next sentence is incredibly confusing.
91 If no arguments are present and if the standard input of the shell
92 is connected to a terminal
96 the shell is considered an interactive shell. An interactive shell
97 generally prompts before each command and handles programming
98 and command errors differently (as described below).
99 When first starting, the shell inspects argument 0, and
100 if it begins with a dash
102 the shell is also considered a login shell.
103 This is normally done automatically by the system
104 when the user first logs in. A login shell first reads commands
109 if they exist. If the environment variable
111 is set on entry to a shell, or is set in the
113 of a login shell, the shell then reads commands from the file named in
115 Therefore, a user should place commands that are to be executed only
118 file, and commands that are executed for every shell inside the
123 variable to some file by placing the following line in the file
125 in the home directory,
128 the filename desired:
130 .Dl ENV=$HOME/.shinit; export ENV
132 The first non-option argument specified on the command line
133 will be treated as the
134 name of a file from which to read commands (a shell script), and
135 the remaining arguments are set as the positional parameters
136 of the shell ($1, $2, etc). Otherwise, the shell reads commands
137 from its standard input.
139 Unlike older versions of
143 script is only sourced on invocation of interactive shells. This
144 closes a well-known, and sometimes easily exploitable security
145 hole related to poorly thought out
148 .Ss Argument List Processing
149 All of the single letter options to
151 have a corresponding long name,
152 with the exception of
156 These long names are provided next to the single letter options
157 in the descriptions below.
158 The long name for an option may be specified as an argument to the
162 Once the shell is running,
163 the long name for an option may be specified as an argument to the
168 (described later in the section called
169 .Sx Built-in Commands ) .
170 Introducing an option with a dash
180 will stop option processing and will force the remaining
181 words on the command line to be treated as arguments.
186 options do not have long names.
187 They take arguments and are described after the single letter options.
188 .Bl -tag -width indent
189 .It Fl a Li allexport
190 Flag variables for export when assignments are made to them.
192 Enable asynchronous notification of background job
195 .It Fl C Li noclobber
196 Do not overwrite existing files with
201 command line editor (disables the
203 option if it has been set).
205 Exit immediately if any untested command fails in non-interactive mode.
206 The exit status of a command is considered to be
207 explicitly tested if the command is used to control
208 an if, elif, while, or until; or if the command is the left
215 Disable pathname expansion.
216 .It Fl I Li ignoreeof
219 from input when in interactive mode.
220 .It Fl i Li interactive
221 Force the shell to behave interactively.
223 Turn on job control (set automatically when interactive).
225 If not interactive, read commands but do not
226 execute them. This is useful for checking the
227 syntax of shell scripts.
229 Change the default for the
235 (logical directory layout)
238 (physical directory layout).
239 .It Fl p Li privileged
240 Turn on privileged mode. This mode is enabled on startup
241 if either the effective user or group id is not equal to the
242 real user or group id. Turning this mode off sets the
243 effective user and group ids to the real user and group ids.
244 When this mode is enabled for interactive shells, the file
245 .Pa /etc/suid_profile
246 is sourced instead of
250 is sourced, and the contents of the
252 variable are ignored.
254 Read commands from standard input (set automatically
255 if no file arguments are present). This option has
256 no effect when set after the shell has already started
257 running (i.e. when set with the
260 .It Fl T Li trapsasync
261 When waiting for a child, execute traps immediately.
262 If this option is not set,
263 traps are executed after the child exits,
266 This nonstandard option is useful for putting guarding shells around
267 children that block signals. The surrounding shell may kill the child
268 or it may just return control to the tty and leave the child alone,
270 .Bd -literal -offset indent
271 sh -T -c "trap 'exit 1' 2 ; some-blocking-program"
275 Write a message to standard error when attempting
276 to expand a variable that is not set, and if the
277 shell is not interactive, exit immediately.
281 command line editor (disables
285 The shell writes its input to standard error
286 as it is read. Useful for debugging.
291 to standard error before it is executed.
292 Useful for debugging.
297 option may be used to pass its string argument to the shell
298 to be interpreted as input.
299 Keep in mind that this option only accepts a single string as its
300 argument, hence multi-word strings must be quoted.
304 option takes as its only argument the long name of an option
305 to be enabled or disabled.
306 For example, the following two invocations of
308 both enable the built-in
311 .Bd -literal -offset indent
316 If used without an argument, the
318 option displays the current option settings in a human-readable format.
321 is used without an argument, the current option settings are output
322 in a format suitable for re-input into the shell.
323 .Ss Lexical Structure
324 The shell reads input in terms of lines from a file and breaks
325 it up into words at whitespace (blanks and tabs), and at
329 which are special to the shell.
330 There are two types of operators: control operators and
331 redirection operators (their meaning is discussed later).
332 The following is a list of valid operators:
333 .Bl -tag -width indent
334 .It Control operators:
335 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
336 .It Li & Ta Li && Ta Li ( Ta Li ) Ta Li \en
337 .It Li ;; Ta Li ; Ta Li | Ta Li ||
339 .It Redirection operators:
340 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
341 .It Li < Ta Li > Ta Li << Ta Li >> Ta Li <>
342 .It Li <& Ta Li >& Ta Li <<- Ta Li >|
346 Quoting is used to remove the special meaning of certain characters
347 or words to the shell, such as operators, whitespace, or
348 keywords. There are three types of quoting: matched single quotes,
349 matched double quotes, and backslash.
350 .Bl -tag -width indent
352 Enclosing characters in single quotes preserves the literal
353 meaning of all the characters (except single quotes, making
354 it impossible to put single-quotes in a single-quoted string).
356 Enclosing characters within double quotes preserves the literal
357 meaning of all characters except dollarsign
363 The backslash inside double quotes is historically weird.
364 It remains literal unless it precedes the following characters,
365 which it serves to quote:
366 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
367 .It Li $ Ta Li ` Ta Li \&" Ta Li \e\ Ta Li \en
370 A backslash preserves the literal meaning of the following
371 character, with the exception of the newline character
373 A backslash preceding a newline is treated as a line continuation.
376 Reserved words are words that have special meaning to the
377 shell and are recognized at the beginning of a line and
378 after a control operator. The following are reserved words:
379 .Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
380 .It Li \&! Ta { Ta } Ta Ic case Ta Ic do
381 .It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
382 .It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
385 An alias is a name and corresponding value set using the
387 built-in command. Whenever a reserved word may occur (see above),
388 and after checking for reserved words, the shell
389 checks the word to see if it matches an alias.
390 If it does, it replaces it in the input stream with its value.
391 For example, if there is an alias called
396 .Bd -literal -offset indent
401 .Bd -literal -offset indent
405 Aliases provide a convenient way for naive users to
406 create shorthands for commands without having to learn how
407 to create functions with arguments. They can also be
408 used to create lexically obscure code. This use is discouraged.
410 The shell interprets the words it reads according to a
411 language, the specification of which is outside the scope
412 of this man page (refer to the BNF in the
414 document). Essentially though, a line is read and if
415 the first word of the line (or after a control operator)
416 is not a reserved word, then the shell has recognized a
417 simple command. Otherwise, a complex command or some
418 other special construct may have been recognized.
420 If a simple command has been recognized, the shell performs
421 the following actions:
424 Leading words of the form
426 are stripped off and assigned to the environment of
427 the simple command. Redirection operators and
428 their arguments (as described below) are stripped
429 off and saved for processing.
431 The remaining words are expanded as described in
433 .Sx Word Expansions ,
434 and the first remaining word is considered the command
435 name and the command is located. The remaining
436 words are considered the arguments of the command.
437 If no command name resulted, then the
439 variable assignments recognized in 1) affect the
442 Redirections are performed as described in
446 Redirections are used to change where a command reads its input
447 or sends its output. In general, redirections open, close, or
448 duplicate an existing reference to a file. The overall format
449 used for redirection is:
451 .Dl [n] redir-op file
455 is one of the redirection operators mentioned
456 previously. The following gives some examples of how these
457 operators can be used.
458 Note that stdin and stdout are commonly used abbreviations
459 for standard input and standard output respectively.
460 .Bl -tag -width "1234567890XX" -offset indent
462 redirect stdout (or file descriptor n) to file
464 same as above, but override the
468 append stdout (or file descriptor n) to file
470 redirect stdin (or file descriptor n) from file
472 redirect stdin (or file descriptor n) to and from file
474 duplicate stdin (or file descriptor n1) from file descriptor n2
476 close stdin (or file descriptor n)
478 duplicate stdout (or file descriptor n1) to file descriptor n2
480 close stdout (or file descriptor n)
483 The following redirection is often called a
485 .Bd -literal -offset indent
492 All the text on successive lines up to the delimiter is
493 saved away and made available to the command on standard
494 input, or file descriptor n if it is specified. If the delimiter
495 as specified on the initial line is quoted, then the here-doc-text
496 is treated literally, otherwise the text is subjected to
497 parameter expansion, command substitution, and arithmetic
498 expansion (as described in the section on
499 .Sx Word Expansions ) .
505 in the here-doc-text are stripped.
506 .Ss Search and Execution
507 There are three types of commands: shell functions,
508 built-in commands, and normal programs.
509 The command is searched for (by name) in that order.
510 The three types of commands are all executed in a different way.
512 When a shell function is executed, all of the shell positional
513 parameters (except $0, which remains unchanged) are
514 set to the arguments of the shell function.
515 The variables which are explicitly placed in the environment of
516 the command (by placing assignments to them before the
517 function name) are made local to the function and are set
519 Then the command given in the function definition is executed.
520 The positional parameters are restored to their original values
521 when the command completes.
522 This all occurs within the current shell.
524 Shell built-in commands are executed internally to the shell, without
525 spawning a new process.
527 Otherwise, if the command name does not match a function
528 or built-in command, the command is searched for as a normal
529 program in the file system (as described in the next section).
530 When a normal program is executed, the shell runs the program,
531 passing the arguments and the environment to the program.
532 If the program is not a normal executable file
533 (i.e. if it does not begin with the
543 the shell will interpret the program in a subshell.
544 The child shell will reinitialize itself in this case,
545 so that the effect will be
546 as if a new shell had been invoked to handle the ad-hoc shell script,
547 except that the location of hashed commands located in
548 the parent shell will be remembered by the child.
550 Note that previous versions of this document
551 and the source code itself misleadingly and sporadically
552 refer to a shell script without a magic number
554 .Qq shell procedure .
556 When locating a command, the shell first looks to see if
557 it has a shell function by that name. Then it looks for a
558 built-in command by that name. If a built-in command is not found,
559 one of two things happen:
562 Command names containing a slash are simply executed without
563 performing any searches.
565 The shell searches each entry in
567 in turn for the command. The value of the
569 variable should be a series of
570 entries separated by colons. Each entry consists of a
572 The current directory
573 may be indicated implicitly by an empty directory name,
574 or explicitly by a single period.
576 .Ss Command Exit Status
577 Each command has an exit status that can influence the behavior
578 of other shell commands. The paradigm is that a command exits
579 with zero for normal or success, and non-zero for failure,
580 error, or a false indication. The man page for each command
581 should indicate the various exit codes and what they mean.
582 Additionally, the built-in commands return exit codes, as does
583 an executed shell function.
585 If a command is terminated by a signal, its exit status is 128 plus
586 the signal number. Signal numbers are defined in the header file
587 .Aq Pa sys/signal.h .
589 Complex commands are combinations of simple commands
590 with control operators or reserved words, together creating a larger complex
591 command. More generally, a command is one of the following:
592 .Bl -item -offset indent
598 list or compound-list
605 Unless otherwise stated, the exit status of a command is
606 that of the last simple command executed by the command.
608 A pipeline is a sequence of one or more commands separated
609 by the control operator |. The standard output of all but
610 the last command is connected to the standard input
611 of the next command. The standard output of the last
612 command is inherited from the shell, as usual.
614 The format for a pipeline is:
616 .Dl [!] command1 [ | command2 ...]
618 The standard output of command1 is connected to the standard
619 input of command2. The standard input, standard output, or
620 both of a command is considered to be assigned by the
621 pipeline before any redirection specified by redirection
622 operators that are part of the command.
624 If the pipeline is not in the background (discussed later),
625 the shell waits for all commands to complete.
627 If the reserved word ! does not precede the pipeline, the
628 exit status is the exit status of the last command specified
629 in the pipeline. Otherwise, the exit status is the logical
630 NOT of the exit status of the last command. That is, if
631 the last command returns zero, the exit status is 1; if
632 the last command returns greater than zero, the exit status
635 Because pipeline assignment of standard input or standard
636 output or both takes place before redirection, it can be
637 modified by redirection. For example:
639 .Dl $ command1 2>&1 | command2
641 sends both the standard output and standard error of
643 to the standard input of
648 or newline terminator causes the preceding
650 (described below in the section called
651 .Sx Short-Circuit List Operators )
652 to be executed sequentially;
655 causes asynchronous execution of the preceding AND-OR-list.
657 Note that unlike some other shells,
659 executes each process in the pipeline as a child of the
662 Shell built-in commands are the exception to this rule.
663 They are executed in the current shell, although they do not affect its
664 environment when used in pipelines.
665 .Ss Background Commands (&)
666 If a command is terminated by the control operator ampersand
668 the shell executes the command asynchronously;
669 the shell does not wait for the command to finish
670 before executing the next command.
672 The format for running a command in background is:
673 .Bd -literal -offset indent
674 command1 & [command2 & ...]
677 If the shell is not interactive, the standard input of an
678 asynchronous command is set to /dev/null.
679 .Ss Lists (Generally Speaking)
680 A list is a sequence of zero or more commands separated by
681 newlines, semicolons, or ampersands,
682 and optionally terminated by one of these three characters.
684 list are executed in the order they are written.
685 If command is followed by an ampersand, the shell starts the
686 command and immediately proceeds onto the next command;
687 otherwise it waits for the command to terminate before
688 proceeding to the next one.
689 .Ss Short-Circuit List Operators
693 are AND-OR list operators.
695 executes the first command, and then executes the second command
696 if the exit status of the first command is zero.
698 is similar, but executes the second command if the exit
699 status of the first command is nonzero.
703 both have the same priority.
704 .Ss Flow-Control Constructs (if, while, for, case)
709 .\" XXX Use .Dl to work around broken handling of .Ic inside .Bd and .Ed .
713 .Dl [ Ic elif Ar list
714 .Dl Ic then Ar list ] ...
715 .Dl [ Ic else Ar list ]
725 The two lists are executed repeatedly while the exit status of the
729 command is similar, but has the word
734 repeat until the exit status of the first list is zero.
739 .Dl Ic for Ar variable Ic in Ar word ...
743 The words are expanded, and then the list is executed
744 repeatedly with the variable set to each word in turn.
749 commands may be replaced with
759 .Dl Ic break Op Ar num
760 .Dl Ic continue Op Ar num
764 command terminates the
773 command continues with the next iteration of the innermost loop.
774 These are implemented as built-in commands.
779 .Dl Ic case Ar word Ic in
784 The pattern can actually be one or more patterns
791 .Ss Grouping Commands Together
792 Commands may be grouped by writing either
793 .Bd -literal -offset indent
798 .Bd -literal -offset indent
802 The first form executes the commands in a subshell.
803 Note that built-in commands thus executed do not affect the current shell.
804 The second form does not fork another shell,
805 so it is slightly more efficient.
806 Grouping commands together this way allows the user to
807 redirect their output as though they were one program:
808 .Bd -literal -offset indent
809 { echo -n "hello"; echo " world"; } > greeting
812 The syntax of a function definition is
813 .Bd -literal -offset indent
817 A function definition is an executable statement; when
818 executed it installs a function named name and returns an
819 exit status of zero. The command is normally a list
825 Variables may be declared to be local to a function by
829 This should appear as the first statement of a function,
831 .Bd -ragged -offset indent
839 command is implemented as a built-in command.
841 When a variable is made local, it inherits the initial
842 value and exported and readonly flags from the variable
843 with the same name in the surrounding scope, if there is
844 one. Otherwise, the variable is initially unset. The shell
845 uses dynamic scoping, so that if the variable
847 is made local to function
849 which then calls function
851 references to the variable
855 will refer to the variable
859 not to the global variable named
862 The only special parameter than can be made local is
866 local causes any shell options that are
867 changed via the set command inside the function to be
868 restored to their original values when the function
874 .Bd -ragged -offset indent
879 It terminates the currently executing function.
882 command is implemented as a built-in command.
883 .Ss Variables and Parameters
884 The shell maintains a set of parameters. A parameter
885 denoted by a name is called a variable. When starting up,
886 the shell turns all the environment variables into shell
887 variables. New variables can be set using the form
888 .Bd -literal -offset indent
892 Variables set by the user must have a name consisting solely
893 of alphabetics, numerics, and underscores.
894 The first letter of a variable name must not be numeric.
895 A parameter can also be denoted by a number
896 or a special character as explained below.
897 .Ss Positional Parameters
898 A positional parameter is a parameter denoted by a number greater than zero.
899 The shell sets these initially to the values of its command line
900 arguments that follow the name of the shell script. The
902 built-in command can also be used to set or reset them.
903 .Ss Special Parameters
904 A special parameter is a parameter denoted by one of the following
905 special characters. The value of the parameter is listed
906 next to its character.
909 Expands to the positional parameters, starting from one. When
910 the expansion occurs within a double-quoted string
911 it expands to a single field with the value of each parameter
912 separated by the first character of the
921 Expands to the positional parameters, starting from one. When
922 the expansion occurs within double-quotes, each positional
923 parameter expands as a separate argument.
924 If there are no positional parameters, the
927 generates zero arguments, even when
929 is double-quoted. What this basically means, for example, is
938 .Bd -literal -offset indent
942 Expands to the number of positional parameters.
944 Expands to the exit status of the most recent pipeline.
946 (hyphen) Expands to the current option flags (the single-letter
947 option names concatenated into a string) as specified on
948 invocation, by the set built-in command, or implicitly
951 Expands to the process ID of the invoked shell. A subshell
952 retains the same value of $ as its parent.
954 Expands to the process ID of the most recent background
955 command executed from the current shell. For a
956 pipeline, the process ID is that of the last command in the
959 (zero) Expands to the name of the shell or shell script.
962 This clause describes the various expansions that are
963 performed on words. Not all expansions are performed on
964 every word, as explained later.
966 Tilde expansions, parameter expansions, command substitutions,
967 arithmetic expansions, and quote removals that occur within
968 a single word expand to a single field. It is only field
969 splitting or pathname expansion that can create multiple
970 fields from a single word.
971 The single exception to this rule is
972 the expansion of the special parameter
974 within double-quotes,
975 as was described above.
977 The order of word expansion is:
980 Tilde Expansion, Parameter Expansion, Command Substitution,
981 Arithmetic Expansion (these all occur at the same time).
983 Field Splitting is performed on fields generated by step (1)
988 Pathname Expansion (unless the
990 option is in effect).
997 character is used to introduce parameter expansion, command
998 substitution, or arithmetic evaluation.
999 .Ss Tilde Expansion (substituting a user's home directory)
1000 A word beginning with an unquoted tilde character
1003 subjected to tilde expansion.
1004 All the characters up to a slash
1006 or the end of the word are treated as a username
1007 and are replaced with the user's home directory. If the
1008 username is missing (as in ~/foobar), the tilde is replaced
1009 with the value of the HOME variable (the current user's
1011 .Ss Parameter Expansion
1012 The format for parameter expansion is as follows:
1013 .Bd -literal -offset indent
1017 where expression consists of all characters until the matching
1021 escaped by a backslash or within a quoted string, and characters in
1022 embedded arithmetic expansions, command substitutions, and variable
1023 expansions, are not examined in determining the matching
1026 The simplest form for parameter expansion is:
1027 .Bd -literal -offset indent
1031 The value, if any, of parameter is substituted.
1033 The parameter name or symbol can be enclosed in braces, which are
1034 optional except for positional parameters with more than one digit or
1035 when parameter is followed by a character that could be interpreted as
1037 If a parameter expansion occurs inside double-quotes:
1040 Pathname expansion is not performed on the results of the
1043 Field splitting is not performed on the results of the
1044 expansion, with the exception of the special parameter
1048 In addition, a parameter expansion can be modified by using one of the
1050 .Bl -tag -width indent
1051 .It Li ${parameter:-word}
1052 Use Default Values. If parameter is unset or
1053 null, the expansion of word is
1054 substituted; otherwise, the value of
1055 parameter is substituted.
1056 .It Li ${parameter:=word}
1057 Assign Default Values. If parameter is unset
1058 or null, the expansion of word is
1059 assigned to parameter. In all cases, the
1060 final value of parameter is
1061 substituted. Only variables, not positional
1062 parameters or special parameters, can be
1063 assigned in this way.
1064 .It Li ${parameter:?[word]}
1065 Indicate Error if Null or Unset. If
1066 parameter is unset or null, the expansion of
1067 word (or a message indicating it is unset if
1068 word is omitted) is written to standard
1069 error and the shell exits with a nonzero
1071 Otherwise, the value of
1072 parameter is substituted. An
1073 interactive shell need not exit.
1074 .It Li ${parameter:+word}
1075 Use Alternate Value. If parameter is unset
1076 or null, null is substituted;
1077 otherwise, the expansion of word is
1081 In the parameter expansions shown previously, use of the colon in the
1082 format results in a test for a parameter that is unset or null; omission
1083 of the colon results in a test for a parameter that is only unset.
1084 .Bl -tag -width indent
1085 .It Li ${#parameter}
1086 String Length. The length in characters of
1087 the value of parameter.
1090 The following four varieties of parameter expansion provide for substring
1092 In each case, pattern matching notation
1094 .Sx Shell Patterns ) ,
1095 rather than regular expression notation,
1096 is used to evaluate the patterns.
1097 If parameter is one of the special parameters
1101 the result of the expansion is unspecified.
1102 Enclosing the full parameter expansion string in double-quotes does not
1103 cause the following four varieties of pattern characters to be quoted,
1104 whereas quoting characters within the braces has this effect.
1105 .Bl -tag -width indent
1106 .It Li ${parameter%word}
1107 Remove Smallest Suffix Pattern. The word
1108 is expanded to produce a pattern. The
1109 parameter expansion then results in
1110 parameter, with the smallest portion of the
1111 suffix matched by the pattern deleted.
1112 .It Li ${parameter%%word}
1113 Remove Largest Suffix Pattern. The word
1114 is expanded to produce a pattern. The
1115 parameter expansion then results in
1116 parameter, with the largest portion of the
1117 suffix matched by the pattern deleted.
1118 .It Li ${parameter#word}
1119 Remove Smallest Prefix Pattern. The word
1120 is expanded to produce a pattern. The
1121 parameter expansion then results in
1122 parameter, with the smallest portion of the
1123 prefix matched by the pattern deleted.
1124 .It Li ${parameter##word}
1125 Remove Largest Prefix Pattern. The word
1126 is expanded to produce a pattern. The
1127 parameter expansion then results in
1128 parameter, with the largest portion of the
1129 prefix matched by the pattern deleted.
1131 .Ss Command Substitution
1132 Command substitution allows the output of a command to be substituted in
1133 place of the command name itself. Command substitution occurs when
1134 the command is enclosed as follows:
1135 .Bd -literal -offset indent
1139 or the backquoted version:
1140 .Bd -literal -offset indent
1144 The shell expands the command substitution by executing command in a
1145 subshell environment and replacing the command substitution
1146 with the standard output of the command,
1147 removing sequences of one or more newlines at the end of the substitution.
1148 Embedded newlines before the end of the output are not removed;
1149 however, during field splitting, they may be translated into spaces
1150 depending on the value of
1152 and the quoting that is in effect.
1153 .Ss Arithmetic Expansion
1154 Arithmetic expansion provides a mechanism for evaluating an arithmetic
1155 expression and substituting its value.
1156 The format for arithmetic expansion is as follows:
1157 .Bd -literal -offset indent
1161 The expression is treated as if it were in double-quotes, except
1162 that a double-quote inside the expression is not treated specially. The
1163 shell expands all tokens in the expression for parameter expansion,
1164 command substitution, and quote removal.
1166 Next, the shell treats this as an arithmetic expression and
1167 substitutes the value of the expression.
1168 .Ss White Space Splitting (Field Splitting)
1169 After parameter expansion, command substitution, and
1170 arithmetic expansion the shell scans the results of
1171 expansions and substitutions that did not occur in double-quotes for
1172 field splitting and multiple fields can result.
1174 The shell treats each character of the
1176 as a delimiter and uses
1177 the delimiters to split the results of parameter expansion and command
1178 substitution into fields.
1179 .Ss Pathname Expansion (File Name Generation)
1183 file name generation is performed
1184 after word splitting is complete. Each word is
1185 viewed as a series of patterns, separated by slashes. The
1186 process of expansion replaces the word with the names of
1187 all existing files whose names can be formed by replacing
1188 each pattern with a string that matches the specified pattern.
1189 There are two restrictions on this: first, a pattern cannot match
1190 a string containing a slash, and second,
1191 a pattern cannot match a string starting with a period
1192 unless the first character of the pattern is a period.
1193 The next section describes the patterns used for both
1194 Pathname Expansion and the
1198 A pattern consists of normal characters, which match themselves,
1199 and meta-characters.
1200 The meta-characters are
1206 These characters lose their special meanings if they are quoted.
1207 When command or variable substitution is performed and the dollar sign
1208 or back quotes are not double-quoted, the value of the
1209 variable or the output of the command is scanned for these
1210 characters and they are turned into meta-characters.
1214 matches any string of characters.
1217 matches any single character.
1220 introduces a character class.
1221 The end of the character class is indicated by a
1229 rather than introducing a character class.
1230 A character class matches any of the characters between the square brackets.
1231 A range of characters may be specified using a minus sign.
1232 The character class may be complemented by making an exclamation point
1234 the first character of the character class.
1238 in a character class, make it the first character listed
1244 make it the first or last character listed.
1245 .Ss Built-in Commands
1246 This section lists the commands which
1247 are built-in because they need to perform some operation
1248 that cannot be performed by a separate process. In addition to
1249 these, built-in versions of the
1253 commands are provided for efficiency.
1254 .Bl -tag -width indent
1256 A null command that returns a 0 (true) exit value.
1258 The commands in the specified file are read and executed by the shell.
1263 characters, it is used as is. Otherwise, the shell searches the
1265 for the file. If it is not found in the
1267 it is sought in the current working directory.
1268 .It Ic alias Op Ar name ...
1269 .It Ic alias Op Ar name Ns = Ns Ar string ...
1271 .Ar name Ns = Ns Ar string
1272 is specified, the shell defines the alias
1278 is specified, the value of the alias
1281 With no arguments, the
1283 built-in command prints the names and values of all defined aliases
1286 Alias values are written with appropriate quoting so that they are
1287 suitable for re-input to the shell.
1288 .It Ic bg Op Ar job ...
1289 Continue the specified jobs
1290 (or the current job if no jobs are given)
1292 .It Ic builtin Ar cmd Op Ar arg ...
1293 Execute the specified built-in command,
1295 This is useful when the user wishes to override a shell function
1296 with the same name as a built-in command.
1297 .It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
1298 List or alter key bindings for the line editor.
1299 This command is documented in
1301 .It Ic cd Oo Fl LP Oc Op Ar directory
1302 Switch to the specified
1304 or to the directory specified in the
1306 environment variable if no
1315 then the directories listed in the
1318 searched for the specified
1322 is unset, the current directory is searched.
1325 is the same as that of
1327 In an interactive shell,
1330 command will print out the name of the directory
1331 that it actually switched to
1332 if this is different from the name that the user gave.
1333 These may be different either because the
1335 mechanism was used or because a symbolic link was crossed.
1339 option is specified,
1341 is handled physically and symbolic links are resolved before
1343 components are processed.
1346 option is specified,
1348 is handled logically.
1349 This is the default.
1354 .It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
1355 Execute the specified
1357 as a simple command (see the
1363 option is specified, the command search is performed using a
1366 that is guaranteed to find all of the standard utilities.
1367 .It Ic echo Oo Fl e | n Oc Op Ar string
1370 to the standard output with a newline appended.
1371 .Bl -tag -width indent
1373 Suppress the output of the trailing newline.
1375 Process C-style backslash escape sequences.
1377 understands the following character escapes:
1378 .Bl -tag -width indent
1380 Alert (ring the terminal bell)
1384 Suppress the trailing newline (this has the side-effect of truncating the
1385 line if it is not the last character)
1387 The ESC character (ASCII 0x1b)
1401 (Zero) The character whose octal value is nnn
1406 is not enclosed in quotes then the backslash itself must be escaped
1407 with a backslash to protect it from the shell. For example
1408 .Bd -literal -offset indent
1417 $ echo -e a\e\e\e\eb
1426 options may be specified.
1427 .It Ic eval Ar string ...
1428 Concatenate all the arguments with spaces.
1429 Then re-parse and execute the command.
1430 .It Ic exec Op Ar command Op arg ...
1434 the shell process is replaced with the specified program
1435 (which must be a real program, not a shell built-in command or function).
1436 Any redirections on the
1438 command are marked as permanent,
1439 so that they are not undone when the
1442 .It Ic exit Op Ar exitstatus
1443 Terminate the shell process.
1447 it is used as the exit status of the shell;
1448 otherwise the exit status of the preceding command is used.
1449 .It Ic export Oo Fl p Oc Op Ar name ...
1450 The specified names are exported so that they will
1451 appear in the environment of subsequent commands.
1452 The only way to un-export a variable is to
1455 The shell allows the value of a variable to be set
1456 at the same time as it is exported by writing
1457 .Bd -literal -offset indent
1461 With no arguments the export command lists the names
1462 of all exported variables.
1465 option is specified, the exported variables are printed as
1466 .Dq Ic export Ar name Ns = Ns Ar value
1467 lines, suitable for re-input to the shell.
1468 .It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
1469 .It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
1470 .It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
1473 built-in command lists, or edits and re-executes,
1474 commands previously entered to an interactive shell.
1475 .Bl -tag -width indent
1477 Use the editor named by
1479 to edit the commands.
1480 The editor string is a command name,
1481 subject to search via the
1486 variable is used as a default when
1491 is null or unset, the value of the
1498 is used as the editor.
1500 List the commands rather than invoking
1501 an editor on them. The commands are written in the
1502 sequence indicated by the first and last operands, as
1505 with each command preceded by the command number.
1507 Suppress command numbers when listing with
1510 Reverse the order of the commands listed
1519 Re-execute the command without invoking an editor.
1522 Select the commands to list or edit.
1523 The number of previous commands that can be accessed
1524 are determined by the value of the
1531 or both are one of the following:
1532 .Bl -tag -width indent
1534 A positive number representing a command number;
1535 command numbers can be displayed with the
1539 A negative decimal number representing the
1540 command that was executed
1543 commands previously.
1544 For example, -1 is the immediately previous command.
1546 A string indicating the most recently entered command
1547 that begins with that string.
1550 operand is not also specified with
1552 the string form of the first operand cannot contain an embedded equal sign.
1556 The following environment variables affect the execution of
1558 .Bl -tag -width indent
1560 Name of the editor to use.
1562 The number of previous commands that are accessible.
1567 or the current job to the foreground.
1568 .It Ic getopts Ar optstring Ar var
1574 command deprecates the older
1577 The first argument should be a series of letters, each possibly
1578 followed by a colon which indicates that the option takes an argument.
1579 The specified variable is set to the parsed option. The index of
1580 the next argument is placed into the shell variable
1582 If an option takes an argument, it is placed into the shell variable
1584 If an invalid option is encountered,
1588 It returns a false value (1) when it encounters the end of the options.
1589 .It Ic hash Oo Fl rv Oc Op Ar command ...
1590 The shell maintains a hash table which remembers the locations of commands.
1591 With no arguments whatsoever, the
1593 command prints out the contents of this table.
1594 Entries which have not been looked at since the last
1596 command are marked with an asterisk;
1597 it is possible for these entries to be invalid.
1601 command removes each specified
1603 from the hash table (unless they are functions) and then locates it.
1608 prints the locations of the commands as it finds them.
1613 command to delete all the entries in the hash table except for functions.
1614 .It Ic jobid Op Ar job
1615 Print the process id's of the processes in the specified
1619 argument is omitted, use the current job.
1620 .It Ic jobs Oo Fl ls Oc Op Ar job ...
1621 Print information about the specified jobs, or all jobs if no
1624 The information printed includes job ID, status and command name.
1628 option is specified, the PID of each job is also printed.
1631 option is specified, only the PIDs of the jobs are printed, one per line.
1633 Print the path of the current directory. The built-in command may
1634 differ from the program of the same name because the
1635 built-in command remembers what the current directory
1636 is rather than recomputing it each time. This makes
1637 it faster. However, if the current directory is
1639 the built-in version of
1641 will continue to print the old name for the directory.
1645 option is specified, symbolic links are resolved.
1648 option is specified, the shell's notion of the current directory
1649 is printed (symbolic links are not resolved).
1650 This is the default.
1651 .It Ic read Oo Fl p Ar prompt Oc Oo Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
1657 and the standard input is a terminal. Then a line is
1658 read from the standard input. The trailing newline
1659 is deleted from the line and the line is split as
1660 described in the section on
1661 .Sx White Space Splitting (Field Splitting)
1663 the pieces are assigned to the variables in order.
1664 If there are more pieces than variables, the remaining
1665 pieces (along with the characters in
1667 that separated them)
1668 are assigned to the last variable.
1669 If there are more variables than pieces, the remaining
1670 variables are assigned the null string.
1672 Backslashes are treated specially, unless the
1675 specified. If a backslash is followed by
1676 a newline, the backslash and the newline will be
1677 deleted. If a backslash is followed by any other
1678 character, the backslash will be deleted and the following
1679 character will be treated as though it were not in
1685 option is specified and the
1687 elapses before any input is supplied,
1690 command will return without assigning any values.
1693 value may optionally be followed by one of
1698 to explicitly specify seconds, minutes or hours.
1699 If none is supplied,
1705 option exists only for backward compatibility with older scripts.
1706 .It Ic readonly Oo Fl p Oc Op Ar name ...
1709 is marked as read only,
1710 so that it cannot be subsequently modified or unset.
1711 The shell allows the value of a variable to be set
1712 at the same time as it is marked read only
1713 by using the following form:
1714 .Bd -literal -offset indent
1718 With no arguments the
1720 command lists the names of all read only variables.
1723 option is specified, the read-only variables are printed as
1724 .Dq Ic readonly Ar name Ns = Ns Ar value
1725 lines, suitable for re-input to the shell.
1726 .It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
1727 .Fl c Ar string Oc Op Fl - Ar arg ...
1730 command performs three different functions:
1733 With no arguments, it lists the values of all shell variables.
1735 If options are given,
1736 either in short form or using the long
1737 .Dq Fl /+o Ar longname
1739 it sets or clears the specified options as described in the section called
1740 .Sx Argument List Processing .
1744 option is specified,
1746 will replace the shell's positional parameters with the subsequent
1748 If no arguments follow the
1751 all the positional parameters will be cleared,
1752 which is equivalent to executing the command
1756 flag may be omitted when specifying arguments to be used
1757 as positional replacement parameters.
1758 This is not recommended,
1759 because the first argument may begin with a dash
1765 command will interpret as a request to enable or disable options.
1767 .It Ic setvar Ar variable Ar value
1768 Assigns the specified
1773 is intended to be used in functions that
1774 assign values to variables whose names are passed as parameters.
1775 In general it is better to write
1776 .Bd -literal -offset indent
1781 .It Ic shift Op Ar n
1782 Shift the positional parameters
1787 A shift sets the value of $1 to the value of $2,
1788 the value of $2 to the value of $3, and so on,
1789 decreasing the value of $# by one.
1790 If there are zero positional parameters, shifting does not do anything.
1791 .It Ic trap Oo Ar action Oc Ar signal ...
1792 Cause the shell to parse and execute
1797 The signals are specified by name or number.
1798 In addition, the pseudo-signal
1800 may be used to specify an action that is performed when the shell terminates.
1803 may be null or omitted;
1804 the former causes the specified signal to be ignored
1805 and the latter causes the default action to be taken.
1806 When the shell forks off a subshell,
1807 it resets trapped (but not ignored) signals to the default action.
1810 command has no effect on signals that were ignored on entry to the shell.
1811 .It Ic type Op Ar name ...
1814 as a command and print the resolution of the command search.
1815 Possible resolutions are:
1816 shell keyword, alias, shell built-in command, command, tracked alias
1818 For aliases the alias expansion is printed;
1819 for commands and tracked aliases
1820 the complete pathname of the command is printed.
1821 .It Ic ulimit Oo Fl HSabcdflmnstuv Oc Op Ar limit
1822 Set or display resource limits (see
1826 is specified, the named resource will be set;
1827 otherwise the current resource value will be displayed.
1831 is specified, the hard limits will be set or displayed.
1832 While everybody is allowed to reduce a hard limit,
1833 only the superuser can increase it.
1837 specifies the soft limits instead. When displaying limits,
1843 The default is to display the soft limits,
1844 and to set both the hard and the soft limits.
1850 command to display all resources.
1853 is not acceptable in this mode.
1855 The remaining options specify which resource value is to be
1856 displayed or modified.
1857 They are mutually exclusive.
1858 .Bl -tag -width indent
1860 The maximum size of socket buffer usage, in bytes.
1861 .It Fl c Ar coredumpsize
1862 The maximal size of core dump files, in 512-byte blocks.
1863 .It Fl d Ar datasize
1864 The maximal size of the data segment of a process, in kilobytes.
1865 .It Fl f Ar filesize
1866 The maximal size of a file, in 512-byte blocks.
1867 .It Fl l Ar lockedmem
1868 The maximal size of memory that can be locked by a process, in
1870 .It Fl m Ar memoryuse
1871 The maximal resident set size of a process, in kilobytes.
1873 The maximal number of descriptors that could be opened by a process.
1874 .It Fl s Ar stacksize
1875 The maximal size of the stack segment, in kilobytes.
1877 The maximal amount of CPU time to be used by each process, in seconds.
1878 .It Fl u Ar userproc
1879 The maximal number of simultaneous processes for this user ID.
1880 .It Fl v Ar virtualmem
1881 The maximal virtual size of a process, in kilobytes.
1883 .It Ic umask Op Ar mask
1884 Set the file creation mask (see
1886 to the octal value specified by
1888 If the argument is omitted, the current mask value is printed.
1889 .It Ic unalias Oo Fl a Oc Op Ar name
1892 is specified, the shell removes that alias.
1895 is specified, all aliases are removed.
1896 .It Ic unset Oo Fl fv Oc Ar name ...
1897 The specified variables or functions are unset and unexported.
1900 option is specified or no options are given, the
1902 arguments are treated as variable names.
1905 option is specified, the
1907 arguments are treated as function names.
1908 .It Ic wait Op Ar job
1909 Wait for the specified
1911 to complete and return the exit status of the last process in the
1913 If the argument is omitted, wait for all jobs to complete
1914 and return an exit status of zero.
1916 .Ss Commandline Editing
1919 is being used interactively from a terminal, the current command
1920 and the command history
1924 .Sx Built-in Commands )
1925 can be edited using vi-mode command line editing.
1926 This mode uses commands similar
1927 to a subset of those described in the vi man page.
1932 enables vi-mode editing and places
1934 into vi insert mode. With vi-mode enabled,
1936 can be switched between insert mode and command mode by typing
1940 while in command mode will pass the line to the shell.
1946 command can be used to enable a subset of
1947 emacs-style command line editing features.