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 $
37 .\" $DragonFly: src/bin/sh/sh.1,v 1.5 2006/07/20 17:01:22 corecode Exp $
44 .Nd command interpreter (shell)
47 .Op Fl /+abCEefIimnPpsTuVvx
48 .Op Fl /+o Ar longname
54 utility is the standard command interpreter for the system.
55 The current version of
57 is in the process of being changed to
60 specification for the shell. This version has many features which make
62 similar in some respects to the Korn shell, but it is not a Korn
66 designated by POSIX, plus a few Berkeley extensions, are being
67 incorporated into this shell.
68 This man page is not intended to be a tutorial nor a complete
69 specification of the shell.
71 The shell is a command that reads lines from
72 either a file or the terminal, interprets them, and
73 generally executes other commands.
74 It is the program that is started when a user logs into the system,
75 although a user can select a different shell with the
79 implements a language that has flow control constructs,
80 a macro facility that provides a variety of features in
81 addition to data storage, along with built-in history and line
82 editing capabilities. 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
85 use (shell scripts). That is, commands can be typed directly
86 to the running shell or can be put into a file,
87 which can be executed directly by the shell.
90 .\" XXX This next sentence is incredibly confusing.
92 If no arguments are present and if the standard input of the shell
93 is connected to a terminal
97 the shell is considered an interactive shell. An interactive shell
98 generally prompts before each command and handles programming
99 and command errors differently (as described below).
100 When first starting, the shell inspects argument 0, and
101 if it begins with a dash
103 the shell is also considered a login shell.
104 This is normally done automatically by the system
105 when the user first logs in. A login shell first reads commands
110 if they exist. If the environment variable
112 is set on entry to a shell, or is set in the
114 of a login shell, the shell then reads commands from the file named in
116 Therefore, a user should place commands that are to be executed only
119 file, and commands that are executed for every shell inside the
124 variable to some file by placing the following line in the file
126 in the home directory,
129 the filename desired:
131 .Dl ENV=$HOME/.shinit; export ENV
133 The first non-option argument specified on the command line
134 will be treated as the
135 name of a file from which to read commands (a shell script), and
136 the remaining arguments are set as the positional parameters
137 of the shell ($1, $2, etc). Otherwise, the shell reads commands
138 from its standard input.
140 Unlike older versions of
144 script is only sourced on invocation of interactive shells. This
145 closes a well-known, and sometimes easily exploitable security
146 hole related to poorly thought out
149 .Ss Argument List Processing
150 All of the single letter options to
152 have a corresponding long name,
153 with the exception of
157 These long names are provided next to the single letter options
158 in the descriptions below.
159 The long name for an option may be specified as an argument to the
163 Once the shell is running,
164 the long name for an option may be specified as an argument to the
169 (described later in the section called
170 .Sx Built-in Commands ) .
171 Introducing an option with a dash
181 will stop option processing and will force the remaining
182 words on the command line to be treated as arguments.
187 options do not have long names.
188 They take arguments and are described after the single letter options.
189 .Bl -tag -width indent
190 .It Fl a Li allexport
191 Flag variables for export when assignments are made to them.
193 Enable asynchronous notification of background job
196 .It Fl C Li noclobber
197 Do not overwrite existing files with
202 command line editor (disables the
204 option if it has been set).
206 Exit immediately if any untested command fails in non-interactive mode.
207 The exit status of a command is considered to be
208 explicitly tested if the command is part of the list used to control
209 an if, elif, while, or until; if the command is the left
214 operator; or if the command is a pipeline preceded by the
217 If a shell function is executed and its exit status is explicitly
218 tested, all commands of the function are considered to be tested as
221 Disable pathname expansion.
222 .It Fl I Li ignoreeof
225 from input when in interactive mode.
226 .It Fl i Li interactive
227 Force the shell to behave interactively.
229 Turn on job control (set automatically when interactive).
231 If not interactive, read commands but do not
232 execute them. This is useful for checking the
233 syntax of shell scripts.
235 Change the default for the
241 (logical directory layout)
244 (physical directory layout).
245 .It Fl p Li privileged
246 Turn on privileged mode. This mode is enabled on startup
247 if either the effective user or group id is not equal to the
248 real user or group id. Turning this mode off sets the
249 effective user and group ids to the real user and group ids.
250 When this mode is enabled for interactive shells, the file
251 .Pa /etc/suid_profile
252 is sourced instead of
256 is sourced, and the contents of the
258 variable are ignored.
260 Read commands from standard input (set automatically
261 if no file arguments are present). This option has
262 no effect when set after the shell has already started
263 running (i.e. when set with the
266 .It Fl T Li trapsasync
267 When waiting for a child, execute traps immediately.
268 If this option is not set,
269 traps are executed after the child exits,
272 This nonstandard option is useful for putting guarding shells around
273 children that block signals. The surrounding shell may kill the child
274 or it may just return control to the tty and leave the child alone,
276 .Bd -literal -offset indent
277 sh -T -c "trap 'exit 1' 2 ; some-blocking-program"
281 Write a message to standard error when attempting
282 to expand a variable that is not set, and if the
283 shell is not interactive, exit immediately.
287 command line editor (disables
291 The shell writes its input to standard error
292 as it is read. Useful for debugging.
297 to standard error before it is executed.
298 Useful for debugging.
303 option may be used to pass its string argument to the shell
304 to be interpreted as input.
305 Keep in mind that this option only accepts a single string as its
306 argument, hence multi-word strings must be quoted.
310 option takes as its only argument the long name of an option
311 to be enabled or disabled.
312 For example, the following two invocations of
314 both enable the built-in
317 .Bd -literal -offset indent
322 If used without an argument, the
324 option displays the current option settings in a human-readable format.
327 is used without an argument, the current option settings are output
328 in a format suitable for re-input into the shell.
329 .Ss Lexical Structure
330 The shell reads input in terms of lines from a file and breaks
331 it up into words at whitespace (blanks and tabs), and at
335 which are special to the shell.
336 There are two types of operators: control operators and
337 redirection operators (their meaning is discussed later).
338 The following is a list of valid operators:
339 .Bl -tag -width indent
340 .It Control operators:
341 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
342 .It Li & Ta Li && Ta Li ( Ta Li ) Ta Li \en
343 .It Li ;; Ta Li ; Ta Li | Ta Li ||
345 .It Redirection operators:
346 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
347 .It Li < Ta Li > Ta Li << Ta Li >> Ta Li <>
348 .It Li <& Ta Li >& Ta Li <<- Ta Li >|
352 Quoting is used to remove the special meaning of certain characters
353 or words to the shell, such as operators, whitespace, or
354 keywords. There are three types of quoting: matched single quotes,
355 matched double quotes, and backslash.
356 .Bl -tag -width indent
358 Enclosing characters in single quotes preserves the literal
359 meaning of all the characters (except single quotes, making
360 it impossible to put single-quotes in a single-quoted string).
362 Enclosing characters within double quotes preserves the literal
363 meaning of all characters except dollarsign
369 The backslash inside double quotes is historically weird.
370 It remains literal unless it precedes the following characters,
371 which it serves to quote:
372 .Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
373 .It Li $ Ta Li ` Ta Li \&" Ta Li \e\ Ta Li \en
376 A backslash preserves the literal meaning of the following
377 character, with the exception of the newline character
379 A backslash preceding a newline is treated as a line continuation.
382 Reserved words are words that have special meaning to the
383 shell and are recognized at the beginning of a line and
384 after a control operator. The following are reserved words:
385 .Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
386 .It Li \&! Ta { Ta } Ta Ic case Ta Ic do
387 .It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
388 .It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
391 An alias is a name and corresponding value set using the
393 built-in command. Whenever a reserved word may occur (see above),
394 and after checking for reserved words, the shell
395 checks the word to see if it matches an alias.
396 If it does, it replaces it in the input stream with its value.
397 For example, if there is an alias called
402 .Bd -literal -offset indent
407 .Bd -literal -offset indent
411 Aliases provide a convenient way for naive users to
412 create shorthands for commands without having to learn how
413 to create functions with arguments. They can also be
414 used to create lexically obscure code. This use is discouraged.
416 The shell interprets the words it reads according to a
417 language, the specification of which is outside the scope
418 of this man page (refer to the BNF in the
420 document). Essentially though, a line is read and if
421 the first word of the line (or after a control operator)
422 is not a reserved word, then the shell has recognized a
423 simple command. Otherwise, a complex command or some
424 other special construct may have been recognized.
426 If a simple command has been recognized, the shell performs
427 the following actions:
430 Leading words of the form
432 are stripped off and assigned to the environment of
433 the simple command. Redirection operators and
434 their arguments (as described below) are stripped
435 off and saved for processing.
437 The remaining words are expanded as described in
439 .Sx Word Expansions ,
440 and the first remaining word is considered the command
441 name and the command is located. The remaining
442 words are considered the arguments of the command.
443 If no command name resulted, then the
445 variable assignments recognized in 1) affect the
448 Redirections are performed as described in
452 Redirections are used to change where a command reads its input
453 or sends its output. In general, redirections open, close, or
454 duplicate an existing reference to a file. The overall format
455 used for redirection is:
457 .Dl [n] redir-op file
461 is one of the redirection operators mentioned
462 previously. The following gives some examples of how these
463 operators can be used.
464 Note that stdin and stdout are commonly used abbreviations
465 for standard input and standard output respectively.
466 .Bl -tag -width "1234567890XX" -offset indent
468 redirect stdout (or file descriptor n) to file
470 same as above, but override the
474 append stdout (or file descriptor n) to file
476 redirect stdin (or file descriptor n) from file
478 redirect stdin (or file descriptor n) to and from file
480 duplicate stdin (or file descriptor n1) from file descriptor n2
482 close stdin (or file descriptor n)
484 duplicate stdout (or file descriptor n1) to file descriptor n2
486 close stdout (or file descriptor n)
489 The following redirection is often called a
491 .Bd -literal -offset indent
498 All the text on successive lines up to the delimiter is
499 saved away and made available to the command on standard
500 input, or file descriptor n if it is specified. If the delimiter
501 as specified on the initial line is quoted, then the here-doc-text
502 is treated literally, otherwise the text is subjected to
503 parameter expansion, command substitution, and arithmetic
504 expansion (as described in the section on
505 .Sx Word Expansions ) .
511 in the here-doc-text are stripped.
512 .Ss Search and Execution
513 There are three types of commands: shell functions,
514 built-in commands, and normal programs.
515 The command is searched for (by name) in that order.
516 The three types of commands are all executed in a different way.
518 When a shell function is executed, all of the shell positional
519 parameters (except $0, which remains unchanged) are
520 set to the arguments of the shell function.
521 The variables which are explicitly placed in the environment of
522 the command (by placing assignments to them before the
523 function name) are made local to the function and are set
525 Then the command given in the function definition is executed.
526 The positional parameters are restored to their original values
527 when the command completes.
528 This all occurs within the current shell.
530 Shell built-in commands are executed internally to the shell, without
531 spawning a new process.
533 Otherwise, if the command name does not match a function
534 or built-in command, the command is searched for as a normal
535 program in the file system (as described in the next section).
536 When a normal program is executed, the shell runs the program,
537 passing the arguments and the environment to the program.
538 If the program is not a normal executable file
539 (i.e. if it does not begin with the
549 the shell will interpret the program in a subshell.
550 The child shell will reinitialize itself in this case,
551 so that the effect will be
552 as if a new shell had been invoked to handle the ad-hoc shell script,
553 except that the location of hashed commands located in
554 the parent shell will be remembered by the child.
556 Note that previous versions of this document
557 and the source code itself misleadingly and sporadically
558 refer to a shell script without a magic number
560 .Qq shell procedure .
562 When locating a command, the shell first looks to see if
563 it has a shell function by that name. Then it looks for a
564 built-in command by that name. If a built-in command is not found,
565 one of two things happen:
568 Command names containing a slash are simply executed without
569 performing any searches.
571 The shell searches each entry in
573 in turn for the command. The value of the
575 variable should be a series of
576 entries separated by colons. Each entry consists of a
578 The current directory
579 may be indicated implicitly by an empty directory name,
580 or explicitly by a single period.
582 .Ss Command Exit Status
583 Each command has an exit status that can influence the behavior
584 of other shell commands. The paradigm is that a command exits
585 with zero for normal or success, and non-zero for failure,
586 error, or a false indication. The man page for each command
587 should indicate the various exit codes and what they mean.
588 Additionally, the built-in commands return exit codes, as does
589 an executed shell function.
591 If a command is terminated by a signal, its exit status is 128 plus
592 the signal number. Signal numbers are defined in the header file
595 Complex commands are combinations of simple commands
596 with control operators or reserved words, together creating a larger complex
597 command. More generally, a command is one of the following:
598 .Bl -item -offset indent
604 list or compound-list
611 Unless otherwise stated, the exit status of a command is
612 that of the last simple command executed by the command.
614 A pipeline is a sequence of one or more commands separated
615 by the control operator |. The standard output of all but
616 the last command is connected to the standard input
617 of the next command. The standard output of the last
618 command is inherited from the shell, as usual.
620 The format for a pipeline is:
622 .Dl [!] command1 [ | command2 ...]
624 The standard output of command1 is connected to the standard
625 input of command2. The standard input, standard output, or
626 both of a command is considered to be assigned by the
627 pipeline before any redirection specified by redirection
628 operators that are part of the command.
630 If the pipeline is not in the background (discussed later),
631 the shell waits for all commands to complete.
633 If the reserved word ! does not precede the pipeline, the
634 exit status is the exit status of the last command specified
635 in the pipeline. Otherwise, the exit status is the logical
636 NOT of the exit status of the last command. That is, if
637 the last command returns zero, the exit status is 1; if
638 the last command returns greater than zero, the exit status
641 Because pipeline assignment of standard input or standard
642 output or both takes place before redirection, it can be
643 modified by redirection. For example:
645 .Dl $ command1 2>&1 | command2
647 sends both the standard output and standard error of
649 to the standard input of
654 or newline terminator causes the preceding
656 (described below in the section called
657 .Sx Short-Circuit List Operators )
658 to be executed sequentially;
661 causes asynchronous execution of the preceding AND-OR-list.
663 Note that unlike some other shells,
665 executes each process in the pipeline as a child of the
668 Shell built-in commands are the exception to this rule.
669 They are executed in the current shell, although they do not affect its
670 environment when used in pipelines.
671 .Ss Background Commands (&)
672 If a command is terminated by the control operator ampersand
674 the shell executes the command asynchronously;
675 the shell does not wait for the command to finish
676 before executing the next command.
678 The format for running a command in background is:
679 .Bd -literal -offset indent
680 command1 & [command2 & ...]
683 If the shell is not interactive, the standard input of an
684 asynchronous command is set to /dev/null.
685 .Ss Lists (Generally Speaking)
686 A list is a sequence of zero or more commands separated by
687 newlines, semicolons, or ampersands,
688 and optionally terminated by one of these three characters.
690 list are executed in the order they are written.
691 If command is followed by an ampersand, the shell starts the
692 command and immediately proceeds onto the next command;
693 otherwise it waits for the command to terminate before
694 proceeding to the next one.
695 .Ss Short-Circuit List Operators
699 are AND-OR list operators.
701 executes the first command, and then executes the second command
702 if the exit status of the first command is zero.
704 is similar, but executes the second command if the exit
705 status of the first command is nonzero.
709 both have the same priority.
710 .Ss Flow-Control Constructs (if, while, for, case)
715 .\" XXX Use .Dl to work around broken handling of .Ic inside .Bd and .Ed .
719 .Dl [ Ic elif Ar list
720 .Dl Ic then Ar list ] ...
721 .Dl [ Ic else Ar list ]
731 The two lists are executed repeatedly while the exit status of the
735 command is similar, but has the word
740 repeat until the exit status of the first list is zero.
745 .Dl Ic for Ar variable Ic in Ar word ...
749 The words are expanded, and then the list is executed
750 repeatedly with the variable set to each word in turn.
755 commands may be replaced with
765 .Dl Ic break Op Ar num
766 .Dl Ic continue Op Ar num
770 command terminates the
779 command continues with the next iteration of the innermost loop.
780 These are implemented as built-in commands.
785 .Dl Ic case Ar word Ic in
790 The pattern can actually be one or more patterns
797 .Ss Grouping Commands Together
798 Commands may be grouped by writing either
799 .Bd -literal -offset indent
804 .Bd -literal -offset indent
808 The first form executes the commands in a subshell.
809 Note that built-in commands thus executed do not affect the current shell.
810 The second form does not fork another shell,
811 so it is slightly more efficient.
812 Grouping commands together this way allows the user to
813 redirect their output as though they were one program:
814 .Bd -literal -offset indent
815 { echo -n "hello"; echo " world"; } > greeting
818 The syntax of a function definition is
819 .Bd -literal -offset indent
823 A function definition is an executable statement; when
824 executed it installs a function named name and returns an
825 exit status of zero. The command is normally a list
831 Variables may be declared to be local to a function by
835 This should appear as the first statement of a function,
837 .Bd -ragged -offset indent
845 command is implemented as a built-in command.
847 When a variable is made local, it inherits the initial
848 value and exported and readonly flags from the variable
849 with the same name in the surrounding scope, if there is
850 one. Otherwise, the variable is initially unset. The shell
851 uses dynamic scoping, so that if the variable
853 is made local to function
855 which then calls function
857 references to the variable
861 will refer to the variable
865 not to the global variable named
868 The only special parameter than can be made local is
872 local causes any shell options that are
873 changed via the set command inside the function to be
874 restored to their original values when the function
880 .Bd -ragged -offset indent
885 It terminates the currently executing function.
888 command is implemented as a built-in command.
889 .Ss Variables and Parameters
890 The shell maintains a set of parameters. A parameter
891 denoted by a name is called a variable. When starting up,
892 the shell turns all the environment variables into shell
893 variables. New variables can be set using the form
894 .Bd -literal -offset indent
898 Variables set by the user must have a name consisting solely
899 of alphabetics, numerics, and underscores.
900 The first letter of a variable name must not be numeric.
901 A parameter can also be denoted by a number
902 or a special character as explained below.
903 .Ss Positional Parameters
904 A positional parameter is a parameter denoted by a number greater than zero.
905 The shell sets these initially to the values of its command line
906 arguments that follow the name of the shell script. The
908 built-in command can also be used to set or reset them.
909 .Ss Special Parameters
910 A special parameter is a parameter denoted by one of the following
911 special characters. The value of the parameter is listed
912 next to its character.
915 Expands to the positional parameters, starting from one. When
916 the expansion occurs within a double-quoted string
917 it expands to a single field with the value of each parameter
918 separated by the first character of the
927 Expands to the positional parameters, starting from one. When
928 the expansion occurs within double-quotes, each positional
929 parameter expands as a separate argument.
930 If there are no positional parameters, the
933 generates zero arguments, even when
935 is double-quoted. What this basically means, for example, is
944 .Bd -literal -offset indent
948 Expands to the number of positional parameters.
950 Expands to the exit status of the most recent pipeline.
952 (hyphen) Expands to the current option flags (the single-letter
953 option names concatenated into a string) as specified on
954 invocation, by the set built-in command, or implicitly
957 Expands to the process ID of the invoked shell. A subshell
958 retains the same value of $ as its parent.
960 Expands to the process ID of the most recent background
961 command executed from the current shell. For a
962 pipeline, the process ID is that of the last command in the
965 (zero) Expands to the name of the shell or shell script.
968 This clause describes the various expansions that are
969 performed on words. Not all expansions are performed on
970 every word, as explained later.
972 Tilde expansions, parameter expansions, command substitutions,
973 arithmetic expansions, and quote removals that occur within
974 a single word expand to a single field. It is only field
975 splitting or pathname expansion that can create multiple
976 fields from a single word.
977 The single exception to this rule is
978 the expansion of the special parameter
980 within double-quotes,
981 as was described above.
983 The order of word expansion is:
986 Tilde Expansion, Parameter Expansion, Command Substitution,
987 Arithmetic Expansion (these all occur at the same time).
989 Field Splitting is performed on fields generated by step (1)
994 Pathname Expansion (unless the
996 option is in effect).
1003 character is used to introduce parameter expansion, command
1004 substitution, or arithmetic evaluation.
1005 .Ss Tilde Expansion (substituting a user's home directory)
1006 A word beginning with an unquoted tilde character
1009 subjected to tilde expansion.
1010 All the characters up to a slash
1012 or the end of the word are treated as a username
1013 and are replaced with the user's home directory. If the
1014 username is missing (as in ~/foobar), the tilde is replaced
1015 with the value of the HOME variable (the current user's
1017 .Ss Parameter Expansion
1018 The format for parameter expansion is as follows:
1019 .Bd -literal -offset indent
1023 where expression consists of all characters until the matching
1027 escaped by a backslash or within a quoted string, and characters in
1028 embedded arithmetic expansions, command substitutions, and variable
1029 expansions, are not examined in determining the matching
1032 The simplest form for parameter expansion is:
1033 .Bd -literal -offset indent
1037 The value, if any, of parameter is substituted.
1039 The parameter name or symbol can be enclosed in braces, which are
1040 optional except for positional parameters with more than one digit or
1041 when parameter is followed by a character that could be interpreted as
1043 If a parameter expansion occurs inside double-quotes:
1046 Pathname expansion is not performed on the results of the
1049 Field splitting is not performed on the results of the
1050 expansion, with the exception of the special parameter
1054 In addition, a parameter expansion can be modified by using one of the
1056 .Bl -tag -width indent
1057 .It Li ${parameter:-word}
1058 Use Default Values. If parameter is unset or
1059 null, the expansion of word is
1060 substituted; otherwise, the value of
1061 parameter is substituted.
1062 .It Li ${parameter:=word}
1063 Assign Default Values. If parameter is unset
1064 or null, the expansion of word is
1065 assigned to parameter. In all cases, the
1066 final value of parameter is
1067 substituted. Only variables, not positional
1068 parameters or special parameters, can be
1069 assigned in this way.
1070 .It Li ${parameter:?[word]}
1071 Indicate Error if Null or Unset. If
1072 parameter is unset or null, the expansion of
1073 word (or a message indicating it is unset if
1074 word is omitted) is written to standard
1075 error and the shell exits with a nonzero
1077 Otherwise, the value of
1078 parameter is substituted. An
1079 interactive shell need not exit.
1080 .It Li ${parameter:+word}
1081 Use Alternate Value. If parameter is unset
1082 or null, null is substituted;
1083 otherwise, the expansion of word is
1087 In the parameter expansions shown previously, use of the colon in the
1088 format results in a test for a parameter that is unset or null; omission
1089 of the colon results in a test for a parameter that is only unset.
1090 .Bl -tag -width indent
1091 .It Li ${#parameter}
1092 String Length. The length in characters of
1093 the value of parameter.
1096 The following four varieties of parameter expansion provide for substring
1098 In each case, pattern matching notation
1100 .Sx Shell Patterns ) ,
1101 rather than regular expression notation,
1102 is used to evaluate the patterns.
1103 If parameter is one of the special parameters
1107 the result of the expansion is unspecified.
1108 Enclosing the full parameter expansion string in double-quotes does not
1109 cause the following four varieties of pattern characters to be quoted,
1110 whereas quoting characters within the braces has this effect.
1111 .Bl -tag -width indent
1112 .It Li ${parameter%word}
1113 Remove Smallest Suffix Pattern. The word
1114 is expanded to produce a pattern. The
1115 parameter expansion then results in
1116 parameter, with the smallest portion of the
1117 suffix matched by the pattern deleted.
1118 .It Li ${parameter%%word}
1119 Remove Largest Suffix Pattern. The word
1120 is expanded to produce a pattern. The
1121 parameter expansion then results in
1122 parameter, with the largest portion of the
1123 suffix matched by the pattern deleted.
1124 .It Li ${parameter#word}
1125 Remove Smallest Prefix Pattern. The word
1126 is expanded to produce a pattern. The
1127 parameter expansion then results in
1128 parameter, with the smallest portion of the
1129 prefix matched by the pattern deleted.
1130 .It Li ${parameter##word}
1131 Remove Largest Prefix Pattern. The word
1132 is expanded to produce a pattern. The
1133 parameter expansion then results in
1134 parameter, with the largest portion of the
1135 prefix matched by the pattern deleted.
1137 .Ss Command Substitution
1138 Command substitution allows the output of a command to be substituted in
1139 place of the command name itself. Command substitution occurs when
1140 the command is enclosed as follows:
1141 .Bd -literal -offset indent
1145 or the backquoted version:
1146 .Bd -literal -offset indent
1150 The shell expands the command substitution by executing command in a
1151 subshell environment and replacing the command substitution
1152 with the standard output of the command,
1153 removing sequences of one or more newlines at the end of the substitution.
1154 Embedded newlines before the end of the output are not removed;
1155 however, during field splitting, they may be translated into spaces
1156 depending on the value of
1158 and the quoting that is in effect.
1159 .Ss Arithmetic Expansion
1160 Arithmetic expansion provides a mechanism for evaluating an arithmetic
1161 expression and substituting its value.
1162 The format for arithmetic expansion is as follows:
1163 .Bd -literal -offset indent
1167 The expression is treated as if it were in double-quotes, except
1168 that a double-quote inside the expression is not treated specially. The
1169 shell expands all tokens in the expression for parameter expansion,
1170 command substitution, and quote removal.
1172 Next, the shell treats this as an arithmetic expression and
1173 substitutes the value of the expression.
1174 .Ss White Space Splitting (Field Splitting)
1175 After parameter expansion, command substitution, and
1176 arithmetic expansion the shell scans the results of
1177 expansions and substitutions that did not occur in double-quotes for
1178 field splitting and multiple fields can result.
1180 The shell treats each character of the
1182 as a delimiter and uses
1183 the delimiters to split the results of parameter expansion and command
1184 substitution into fields.
1185 .Ss Pathname Expansion (File Name Generation)
1189 file name generation is performed
1190 after word splitting is complete. Each word is
1191 viewed as a series of patterns, separated by slashes. The
1192 process of expansion replaces the word with the names of
1193 all existing files whose names can be formed by replacing
1194 each pattern with a string that matches the specified pattern.
1195 There are two restrictions on this: first, a pattern cannot match
1196 a string containing a slash, and second,
1197 a pattern cannot match a string starting with a period
1198 unless the first character of the pattern is a period.
1199 The next section describes the patterns used for both
1200 Pathname Expansion and the
1204 A pattern consists of normal characters, which match themselves,
1205 and meta-characters.
1206 The meta-characters are
1212 These characters lose their special meanings if they are quoted.
1213 When command or variable substitution is performed and the dollar sign
1214 or back quotes are not double-quoted, the value of the
1215 variable or the output of the command is scanned for these
1216 characters and they are turned into meta-characters.
1220 matches any string of characters.
1223 matches any single character.
1226 introduces a character class.
1227 The end of the character class is indicated by a
1235 rather than introducing a character class.
1236 A character class matches any of the characters between the square brackets.
1237 A range of characters may be specified using a minus sign.
1238 The character class may be complemented by making an exclamation point
1242 the first character of the character class.
1246 in a character class, make it the first character listed
1254 make it the first or last character listed.
1255 .Ss Built-in Commands
1256 This section lists the commands which
1257 are built-in because they need to perform some operation
1258 that cannot be performed by a separate process. In addition to
1259 these, built-in versions of the
1263 commands are provided for efficiency.
1264 .Bl -tag -width indent
1266 A null command that returns a 0 (true) exit value.
1268 The commands in the specified file are read and executed by the shell.
1273 characters, it is used as is. Otherwise, the shell searches the
1275 for the file. If it is not found in the
1277 it is sought in the current working directory.
1278 .It Ic alias Op Ar name ...
1279 .It Ic alias Op Ar name Ns = Ns Ar string ...
1281 .Ar name Ns = Ns Ar string
1282 is specified, the shell defines the alias
1288 is specified, the value of the alias
1291 With no arguments, the
1293 built-in command prints the names and values of all defined aliases
1296 Alias values are written with appropriate quoting so that they are
1297 suitable for re-input to the shell.
1298 .It Ic bg Op Ar job ...
1299 Continue the specified jobs
1300 (or the current job if no jobs are given)
1302 .It Ic builtin Ar cmd Op Ar arg ...
1303 Execute the specified built-in command,
1305 This is useful when the user wishes to override a shell function
1306 with the same name as a built-in command.
1307 .It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
1308 List or alter key bindings for the line editor.
1309 This command is documented in
1311 .It Ic cd Oo Fl LP Oc Op Ar directory
1312 Switch to the specified
1314 or to the directory specified in the
1316 environment variable if no
1325 then the directories listed in the
1328 searched for the specified
1332 is unset, the current directory is searched.
1335 is the same as that of
1337 In an interactive shell,
1340 command will print out the name of the directory
1341 that it actually switched to
1342 if this is different from the name that the user gave.
1343 These may be different either because the
1345 mechanism was used or because a symbolic link was crossed.
1349 option is specified,
1351 is handled physically and symbolic links are resolved before
1353 components are processed.
1356 option is specified,
1358 is handled logically.
1359 This is the default.
1364 .It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
1365 Execute the specified
1367 as a simple command (see the
1373 option is specified, the command search is performed using a
1376 that is guaranteed to find all of the standard utilities.
1377 .It Ic echo Oo Fl e | n Oc Op Ar string
1380 to the standard output with a newline appended.
1381 .Bl -tag -width indent
1383 Suppress the output of the trailing newline.
1385 Process C-style backslash escape sequences.
1387 understands the following character escapes:
1388 .Bl -tag -width indent
1390 Alert (ring the terminal bell)
1394 Suppress the trailing newline (this has the side-effect of truncating the
1395 line if it is not the last character)
1397 The ESC character (ASCII 0x1b)
1411 (Zero) The character whose octal value is nnn
1416 is not enclosed in quotes then the backslash itself must be escaped
1417 with a backslash to protect it from the shell. For example
1418 .Bd -literal -offset indent
1427 $ echo -e a\e\e\e\eb
1436 options may be specified.
1437 .It Ic eval Ar string ...
1438 Concatenate all the arguments with spaces.
1439 Then re-parse and execute the command.
1440 .It Ic exec Op Ar command Op arg ...
1444 the shell process is replaced with the specified program
1445 (which must be a real program, not a shell built-in command or function).
1446 Any redirections on the
1448 command are marked as permanent,
1449 so that they are not undone when the
1452 .It Ic exit Op Ar exitstatus
1453 Terminate the shell process.
1457 it is used as the exit status of the shell;
1458 otherwise the exit status of the preceding command is used.
1459 .It Ic export Oo Fl p Oc Op Ar name ...
1460 The specified names are exported so that they will
1461 appear in the environment of subsequent commands.
1462 The only way to un-export a variable is to
1465 The shell allows the value of a variable to be set
1466 at the same time as it is exported by writing
1467 .Bd -literal -offset indent
1471 With no arguments the export command lists the names
1472 of all exported variables.
1475 option is specified, the exported variables are printed as
1476 .Dq Ic export Ar name Ns = Ns Ar value
1477 lines, suitable for re-input to the shell.
1478 .It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
1479 .It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
1480 .It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
1483 built-in command lists, or edits and re-executes,
1484 commands previously entered to an interactive shell.
1485 .Bl -tag -width indent
1487 Use the editor named by
1489 to edit the commands.
1490 The editor string is a command name,
1491 subject to search via the
1496 variable is used as a default when
1501 is null or unset, the value of the
1508 is used as the editor.
1510 List the commands rather than invoking
1511 an editor on them. The commands are written in the
1512 sequence indicated by the first and last operands, as
1515 with each command preceded by the command number.
1517 Suppress command numbers when listing with
1520 Reverse the order of the commands listed
1529 Re-execute the command without invoking an editor.
1532 Select the commands to list or edit.
1533 The number of previous commands that can be accessed
1534 are determined by the value of the
1541 or both are one of the following:
1542 .Bl -tag -width indent
1544 A positive number representing a command number;
1545 command numbers can be displayed with the
1549 A negative decimal number representing the
1550 command that was executed
1553 commands previously.
1554 For example, -1 is the immediately previous command.
1556 A string indicating the most recently entered command
1557 that begins with that string.
1560 operand is not also specified with
1562 the string form of the first operand cannot contain an embedded equal sign.
1566 The following environment variables affect the execution of
1568 .Bl -tag -width indent
1570 Name of the editor to use.
1572 The number of previous commands that are accessible.
1577 or the current job to the foreground.
1578 .It Ic getopts Ar optstring Ar var
1584 command deprecates the older
1587 The first argument should be a series of letters, each possibly
1588 followed by a colon which indicates that the option takes an argument.
1589 The specified variable is set to the parsed option. The index of
1590 the next argument is placed into the shell variable
1592 If an option takes an argument, it is placed into the shell variable
1594 If an invalid option is encountered,
1598 It returns a false value (1) when it encounters the end of the options.
1599 .It Ic hash Oo Fl rv Oc Op Ar command ...
1600 The shell maintains a hash table which remembers the locations of commands.
1601 With no arguments whatsoever, the
1603 command prints out the contents of this table.
1604 Entries which have not been looked at since the last
1606 command are marked with an asterisk;
1607 it is possible for these entries to be invalid.
1611 command removes each specified
1613 from the hash table (unless they are functions) and then locates it.
1618 prints the locations of the commands as it finds them.
1623 command to delete all the entries in the hash table except for functions.
1624 .It Ic jobid Op Ar job
1625 Print the process id's of the processes in the specified
1629 argument is omitted, use the current job.
1630 .It Ic jobs Oo Fl ls Oc Op Ar job ...
1631 Print information about the specified jobs, or all jobs if no
1634 The information printed includes job ID, status and command name.
1638 option is specified, the PID of each job is also printed.
1641 option is specified, only the PIDs of the jobs are printed, one per line.
1643 Print the path of the current directory. The built-in command may
1644 differ from the program of the same name because the
1645 built-in command remembers what the current directory
1646 is rather than recomputing it each time. This makes
1647 it faster. However, if the current directory is
1649 the built-in version of
1651 will continue to print the old name for the directory.
1655 option is specified, symbolic links are resolved.
1658 option is specified, the shell's notion of the current directory
1659 is printed (symbolic links are not resolved).
1660 This is the default.
1661 .It Ic read Oo Fl p Ar prompt Oc Oo Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
1667 and the standard input is a terminal. Then a line is
1668 read from the standard input. The trailing newline
1669 is deleted from the line and the line is split as
1670 described in the section on
1671 .Sx White Space Splitting (Field Splitting)
1673 the pieces are assigned to the variables in order.
1674 If there are more pieces than variables, the remaining
1675 pieces (along with the characters in
1677 that separated them)
1678 are assigned to the last variable.
1679 If there are more variables than pieces, the remaining
1680 variables are assigned the null string.
1682 Backslashes are treated specially, unless the
1685 specified. If a backslash is followed by
1686 a newline, the backslash and the newline will be
1687 deleted. If a backslash is followed by any other
1688 character, the backslash will be deleted and the following
1689 character will be treated as though it were not in
1695 option is specified and the
1697 elapses before any input is supplied,
1700 command will return without assigning any values.
1703 value may optionally be followed by one of
1708 to explicitly specify seconds, minutes or hours.
1709 If none is supplied,
1715 option exists only for backward compatibility with older scripts.
1716 .It Ic readonly Oo Fl p Oc Op Ar name ...
1719 is marked as read only,
1720 so that it cannot be subsequently modified or unset.
1721 The shell allows the value of a variable to be set
1722 at the same time as it is marked read only
1723 by using the following form:
1724 .Bd -literal -offset indent
1728 With no arguments the
1730 command lists the names of all read only variables.
1733 option is specified, the read-only variables are printed as
1734 .Dq Ic readonly Ar name Ns = Ns Ar value
1735 lines, suitable for re-input to the shell.
1736 .It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
1737 .Fl c Ar string Oc Op Fl - Ar arg ...
1740 command performs three different functions:
1743 With no arguments, it lists the values of all shell variables.
1745 If options are given,
1746 either in short form or using the long
1747 .Dq Fl /+o Ar longname
1749 it sets or clears the specified options as described in the section called
1750 .Sx Argument List Processing .
1754 option is specified,
1756 will replace the shell's positional parameters with the subsequent
1758 If no arguments follow the
1761 all the positional parameters will be cleared,
1762 which is equivalent to executing the command
1766 flag may be omitted when specifying arguments to be used
1767 as positional replacement parameters.
1768 This is not recommended,
1769 because the first argument may begin with a dash
1775 command will interpret as a request to enable or disable options.
1777 .It Ic setvar Ar variable Ar value
1778 Assigns the specified
1783 is intended to be used in functions that
1784 assign values to variables whose names are passed as parameters.
1785 In general it is better to write
1786 .Bd -literal -offset indent
1791 .It Ic shift Op Ar n
1792 Shift the positional parameters
1797 A shift sets the value of $1 to the value of $2,
1798 the value of $2 to the value of $3, and so on,
1799 decreasing the value of $# by one.
1800 If there are zero positional parameters, shifting does not do anything.
1801 .It Ic trap Oo Ar action Oc Ar signal ...
1802 Cause the shell to parse and execute
1807 The signals are specified by name or number.
1808 In addition, the pseudo-signal
1810 may be used to specify an action that is performed when the shell terminates.
1813 may be null or omitted;
1814 the former causes the specified signal to be ignored
1815 and the latter causes the default action to be taken.
1816 When the shell forks off a subshell,
1817 it resets trapped (but not ignored) signals to the default action.
1820 command has no effect on signals that were ignored on entry to the shell.
1821 .It Ic type Op Ar name ...
1824 as a command and print the resolution of the command search.
1825 Possible resolutions are:
1826 shell keyword, alias, shell built-in command, command, tracked alias
1828 For aliases the alias expansion is printed;
1829 for commands and tracked aliases
1830 the complete pathname of the command is printed.
1831 .It Ic ulimit Oo Fl HSabcdflmnstuv Oc Op Ar limit
1832 Set or display resource limits (see
1836 is specified, the named resource will be set;
1837 otherwise the current resource value will be displayed.
1841 is specified, the hard limits will be set or displayed.
1842 While everybody is allowed to reduce a hard limit,
1843 only the superuser can increase it.
1847 specifies the soft limits instead. When displaying limits,
1853 The default is to display the soft limits,
1854 and to set both the hard and the soft limits.
1860 command to display all resources.
1863 is not acceptable in this mode.
1865 The remaining options specify which resource value is to be
1866 displayed or modified.
1867 They are mutually exclusive.
1868 .Bl -tag -width indent
1870 The maximum size of socket buffer usage, in bytes.
1871 .It Fl c Ar coredumpsize
1872 The maximal size of core dump files, in 512-byte blocks.
1873 .It Fl d Ar datasize
1874 The maximal size of the data segment of a process, in kilobytes.
1875 .It Fl f Ar filesize
1876 The maximal size of a file, in 512-byte blocks.
1877 .It Fl l Ar lockedmem
1878 The maximal size of memory that can be locked by a process, in
1880 .It Fl m Ar memoryuse
1881 The maximal resident set size of a process, in kilobytes.
1883 The maximal number of descriptors that could be opened by a process.
1884 .It Fl s Ar stacksize
1885 The maximal size of the stack segment, in kilobytes.
1887 The maximal amount of CPU time to be used by each process, in seconds.
1888 .It Fl u Ar userproc
1889 The maximal number of simultaneous processes for this user ID.
1890 .It Fl v Ar virtualmem
1891 The maximal virtual size of a process, in kilobytes.
1893 .It Ic umask Op Ar mask
1894 Set the file creation mask (see
1896 to the octal value specified by
1898 If the argument is omitted, the current mask value is printed.
1899 .It Ic unalias Oo Fl a Oc Op Ar name
1902 is specified, the shell removes that alias.
1905 is specified, all aliases are removed.
1906 .It Ic unset Oo Fl fv Oc Ar name ...
1907 The specified variables or functions are unset and unexported.
1910 option is specified or no options are given, the
1912 arguments are treated as variable names.
1915 option is specified, the
1917 arguments are treated as function names.
1918 .It Ic wait Op Ar job
1919 Wait for the specified
1921 to complete and return the exit status of the last process in the
1923 If the argument is omitted, wait for all jobs to complete
1924 and return an exit status of zero.
1926 .Ss Commandline Editing
1929 is being used interactively from a terminal, the current command
1930 and the command history
1934 .Sx Built-in Commands )
1935 can be edited using vi-mode command line editing.
1936 This mode uses commands similar
1937 to a subset of those described in the vi man page.
1942 enables vi-mode editing and places
1944 into vi insert mode. With vi-mode enabled,
1946 can be switched between insert mode and command mode by typing
1950 while in command mode will pass the line to the shell.
1956 command can be used to enable a subset of
1957 emacs-style command line editing features.