Penguin
Blame: bash(1)Part4
EditPageHistoryDiffInfoLikePages
Annotated edit history of bash(1)Part4 version 1, including all changes. View license author blame.
Rev Author # Line
1 JohnMcPherson 1 !!BASH MAN PAGE (Part 4)
2
3 __Navigation__%%%
4 This man page has been split into 7 pages:%%%
5 bash(1)
6 * NAME
7 * SYNOPSIS
8 * COPYRIGHT
9 * DESCRIPTION
10 * OPTIONS
11 * ARGUMENTS
12 * INVOCATION
13
14 [bash(1)Part2]
15 * DEFINITIONS
16 * RESERVED WORDS
17 * SHELL GRAMMAR
18 * COMMENTS
19 * QUOTING
20
21 [bash(1)Part3]
22 * PARAMETERS
23
24 __Part 4__
25 * __EXPANSION__
26
27 [bash(1)Part5]
28 * REDIRECTION
29 * ALIASES
30 * FUNCTIONS
31 * ARITHMETIC EVALUATION
32 * CONDITIONAL EXPRESSIONS
33 * SIMPLE COMMAND EXPANSION
34 * COMMAND EXECUTION
35 * COMMAND EXECUTION ENVIRONMENT
36 * ENVIRONMENT
37 * EXIT STATUS
38 * SIGNALS
39 * JOB CONTROL
40 * PROMPTING
41
42 [bash(1)Part6]
43 * READLINE
44 * HISTORY
45 * HISTORY EXPANSION
46
47
48 [bash(1)Part7]
49 * SHELL BUILTIN COMMANDS
50 * RESTRICTED SHELL
51 * SEE ALSO
52 * FILES
53 * AUTHORS
54 * BUG REPORTS
55 * BUGS
56
57 ----
58 !!EXPANSION
59 Expansion is performed on the command line after it has been split into words. There are seven kinds of expansion performed: ''brace expansion'' , ''tilde expansion'' , ''parameter and variable expansion'' , ''command substitution'' , ''arithmetic expansion'' , ''word splitting'' , and ''pathname expansion'' .
60
61 The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.
62
63 On systems that can support it, there is an additional expansion available: ''process substitution''.
64
65 Only brace expansion, word splitting, and pathname expansion can change the number of words of the expansion; other expansions expand a single word to a single word. The only exceptions to this are the expansions of "__$@__" and "__${__''name''__[[@]}__" as explained above (see __PARAMETERS__ ).
66
67 !Brace Expansion
68
69
70 ''Brace expansion'' is a mechanism by which arbitrary strings may be generated. This mechanism is similar to ''pathname expansion'', but the filenames generated need not exist. Patterns to be brace expanded take the form of an optional ''preamble'' , followed by a series of comma-separated strings between a pair of braces, followed by an optional ''postscript'' . The preamble is prefixed to each string contained within the braces, and the postscript is then appended to each resulting string, expanding left to right.
71
72 Brace expansions may be nested. The results of each expanded string are not sorted; left to right order is preserved. For example, a__{__d,c,b__}__e expands into `ade ace abe'.
73
74 Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. __Bash__ does not apply any syntactic interpretation to the context of the expansion or the text between the braces.
75
76 A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma. Any incorrectly formed brace expansion is left unchanged. A __{__ or __,__ may be quoted with a backslash to prevent its being considered part of a brace expression. To avoid conflicts with parameter expansion, the string __${__ is not considered eligible for brace expansion.
77
78 This construct is typically used as shorthand when the common prefix of the strings to be generated is longer than in the above example:
79
80 mkdir /usr/local/src/bash/{old,new,dist,bugs}
81 or
82 chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
83
84
85 Brace expansion introduces a slight incompatibility with historical versions of __sh__ . __sh__ does not treat opening or closing braces specially when they appear as part of a word, and preserves them in the output. __Bash__ removes braces from words as a consequence of brace expansion. For example, a word entered to __sh__ as ''file{1,2}'' appears identically in the output. The same word is output as ''file1 file2'' after expansion by __bash__ . If strict compatibility with __sh__ is desired, start __bash__ with the __+B __ option or disable brace expansion with the __+B__ option to the __set__ command (see __SHELL BUILTIN COMMANDS__ below).
86
87 !Tilde Expansion
88
89
90 If a word begins with an unquoted tilde character (`__~__'), all of the characters preceding the first unquoted slash (or all characters, if there is no unquoted slash) are considered a ''tilde-prefix''. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible ''login name''. If this login name is the null string, the tilde is replaced with the value of the shell parameter __HOME__ . If __HOME__ is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.
91
92 If the tilde-prefix is a `~+', the value of the shell variable __PWD__ replaces the tilde-prefix. If the tilde-prefix is a `~-', the value of the shell variable __OLDPWD__ , if it is set, is substituted. If the characters following the tilde in the tilde-prefix consist of a number ''N'', optionally prefixed by a `+' or a `-', the tilde-prefix is replaced with the corresponding element from the directory stack, as it would be displayed by the __dirs__ builtin invoked with the tilde-prefix as an argument. If the characters following the tilde in the tilde-prefix consist of a number without a leading `+' or `-', `+' is assumed.
93
94 If the login name is invalid, or the tilde expansion fails, the word is unchanged.
95
96 Each variable assignment is checked for unquoted tilde-prefixes immediately following a __:__ or __=__ . In these cases, tilde expansion is also performed. Consequently, one may use file names with tildes in assignments to __PATH__ , __MAILPATH__ , and __CDPATH__ , and the shell assigns the expanded value.
97
98 !Parameter Expansion
99
100
101 The `__$__' character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.
102
103 When braces are used, the matching ending brace is the first `__}__' not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or paramter expansion.
104
105
106
107
108 ;${''parameter''} : The value of ''parameter'' is substituted. The braces are required when ''parameter'' is a positional parameter with more than one digit, or when ''parameter'' is followed by a character which is not to be interpreted as part of its name.
109
110
111
112 If the first character of ''parameter'' is an exclamation point, a level of variable indirection is introduced. __Bash__ uses the value of the variable formed from the rest of ''parameter'' as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of ''parameter'' itself. This is known as ''indirect expansion''. The exception to this is the expansion of ${!''prefix''*} described below.
113
114 In each of the cases below, ''word'' is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion. When not performing substring expansion, __bash__ tests for a parameter that is unset or null; omitting the colon results in a test only for a parameter that is unset.
115
116
117 ${''parameter''__:-__''word''}
118 ;: __Use Default Values__. If ''parameter'' is unset or null, the expansion of ''word'' is substituted. Otherwise, the value of ''parameter'' is substituted.
119
120 ${''parameter''__:=__''word''}
121 ;: __Assign Default Values__. If ''parameter'' is unset or null, the expansion of ''word'' is assigned to ''parameter'' . The value of ''parameter'' is then substituted. Positional parameters and special parameters may not be assigned to in this way.
122
123 ${''parameter''__:?__''word''}
124 ;: __Display Error if Null or Unset__. If ''parameter'' is null or unset, the expansion of ''word'' (or a message to that effect if ''word'' is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of ''parameter'' is substituted.
125
126 ${''parameter''__:+__''word''}
127 ;: __Use Alternate Value__. If ''parameter'' is null or unset, nothing is substituted, otherwise the expansion of ''word'' is substituted.
128
129 ;: ${''parameter''__:__''offset''}
130
131 ${''parameter''__:__''offset''__:__''length''}
132 ;: __Substring Expansion.__ Expands to up to ''length'' characters of ''parameter'' starting at the character specified by ''offset''. If ''length'' is omitted, expands to the substring of ''parameter'' starting at the character specified by ''offset''. ''length'' and ''offset'' are arithmetic expressions (see .B ARITHMETIC EVALUATION below). ''length'' must evaluate to a number greater than or equal to zero. If ''offset'' evaluates to a number less than zero, the value is used as an offset from the end of the value of ''parameter''. If ''parameter'' is __@__, the result is ''length'' positional parameters beginning at ''offset''. If ''parameter'' is an array name indexed by @ or *, the result is the ''length'' members of the array beginning with ${''parameter''[[''offset'']}. Substring indexing is zero-based unless the positional parameters are used, in which case the indexing starts at 1.
133 ;${__!__''prefix''__*__} : Expands to the names of variables whose names begin with ''prefix'', separated by the first character of the __IFS__ special variable.
134 ;${__#__''parameter''} : The length in characters of the value of ''parameter'' is substituted. If ''parameter'' is __*__ or __@__ , the value substituted is the number of positional parameters. If ''parameter'' is an array name subscripted by __*__ or __@__ , the value substituted is the number of elements in the array.
135
136 ${''parameter''__#__''word''}
137 ;${''parameter''__##__''word''} :
138 The ''word'' is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of ''parameter'' , then the result of the expansion is the expanded value of ''parameter'' with the shortest matching pattern (the "__#__" case) or the longest matching pattern (the "__##__" case) deleted. If ''parameter'' is __@__ or __*__ , the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If ''parameter'' is an array variable subscripted with __@__ or __*__ , the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
139
140 ${''parameter''__%__''word''}
141 ;${''parameter''__%%__''word''}: The ''word'' is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of ''parameter'' , then the result of the expansion is the expanded value of ''parameter'' with the shortest matching pattern (the "__%__" case) or the longest matching pattern (the "__%%__" case) deleted. If ''parameter'' is __@__ or __*__ , the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If ''parameter'' is an array variable subscripted with __@__ or __*__ , the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
142
143 ${''parameter''__/__''pattern''__/__''string''}
144 ;${''parameter''__//__''pattern''__/__''string''}: The ''pattern'' is expanded to produce a pattern just as in pathname expansion. ''Parameter'' is expanded and the longest match of ''pattern'' against its value is replaced with ''string''. In the first form, only the first match is replaced. The second form causes all matches of ''pattern'' to be replaced with ''string''. If ''pattern'' begins with __#__, it must match at the beginning of the expanded value of ''parameter''. If ''pattern'' begins with __%__, it must match at the end of the expanded value of ''parameter''. If ''string'' is null, matches of ''pattern'' are deleted and the __/__ following ''pattern'' may be omitted. If ''parameter'' is __@__ or __*__ , the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If ''parameter'' is an array variable subscripted with __@__ or __*__ , the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
145
146 !Command Substitution
147
148
149 ''Command substitution'' allows the output of a command to replace the command name. There are two forms:
150
151
152 __$(__''command''__)__
153 or
154 __`__''command''__`__
155
156
157 __Bash__ performs the expansion by executing ''command'' and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution __$(cat ''file''__) can be replaced by the equivalent but faster __$(< ''file''__).
158
159 When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by __$__ , __`__ , or __\__ . The first backquote not preceded by a backslash terminates the command substitution. When using the $(''command'') form, all characters between the parentheses make up the command; none are treated specially.
160
161 Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with backslashes.
162
163 If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results.
164
165 !Arithmetic Expansion
166
167
168 Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:
169
170 __$((__''expression''__))__
171
172
173 The ''expression'' is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter expansion, string expansion, command substitution, and quote removal. Arithmetic substitutions may be nested.
174
175 The evaluation is performed according to the rules listed below under __ARITHMETIC EVALUATION__ . If ''expression'' is invalid, __bash__ prints a message indicating failure and no substitution occurs.
176
177 !Process Substitution
178
179
180 ''Process substitution'' is supported on systems that support named pipes (''FIFOs'') or the __/dev/fd__ method of naming open files. It takes the form of __<(__''list''__)__ or __>(__''list''__)__. The process ''list'' is run with its input or output connected to a ''FIFO'' or some file in __/dev/fd__. The name of this file is passed as an argument to the current command as the result of the expansion. If the __>(__''list''__)__ form is used, writing to the file will provide input for ''list''. If the __<(__''list''__)__ form is used, the file passed as an argument should be read to obtain the output of ''list''.
181
182 When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion.
183
184 !Word Splitting
185
186
187 The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for ''word splitting'' .
188
189 The shell treats each character of __IFS__ as a delimiter, and splits the results of the other expansions into words on these characters. If __IFS__ is unset, or its value is exactly __<space><tab><newline>__ , the default, then any sequence of __IFS__ characters serves to delimit words. If __IFS__ has a value other than the default, then sequences of the whitespace characters __space__ and __tab__ are ignored at the beginning and end of the word, as long as the whitespace character is in the value of __IFS__ (an __IFS__ whitespace character). Any character in __IFS__ that is not __IFS__ whitespace, along with any adjacent __IFS__ whitespace characters, delimits a field. A sequence of __IFS__ whitespace characters is also treated as a delimiter. If the value of __IFS__ is null, no word splitting occurs.
190
191 Explicit null arguments ("" or '') are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.
192
193 Note that if no expansion occurs, no splitting is performed.
194
195 !Pathname Expansion
196
197
198 After word splitting, unless the __-f__ option has been set, __bash__ scans each word for the characters __*__ , __?__ , and __[[__ . If one of these characters appears, then the word is regarded as a ''pattern'' , and replaced with an alphabetically sorted list of file names matching the pattern. If no matching file names are found, and the shell option __nullglob__ is disabled, the word is left unchanged. If the __nullglob__ option is set, and no matches are found, the word is removed. If the shell option __nocaseglob__ is enabled, the match is performed without regard to the case of alphabetic characters. When a pattern is used for pathname expansion, the character __.__ at the start of a name or immediately following a slash must be matched explicitly, unless the shell option __dotglob__ is set. When matching a pathname, the slash character must always be matched explicitly. In other cases, the __.__ character is not treated specially. See the description of __shopt__ below under __SHELL BUILTIN COMMANDS__ for a description of the __nocaseglob__ , __nullglob__ , and __dotglob__ shell options.
199
200 The __GLOBIGNORE__ shell variable may be used to restrict the set of file names matching a ''pattern'' . If __GLOBIGNORE__ is set, each matching file name that also matches one of the patterns in __GLOBIGNORE__ is removed from the list of matches. The file names __.__ and __..__ are always ignored, even when __GLOBIGNORE__ is set. However, setting __GLOBIGNORE__ has the effect of enabling the __dotglob__ shell option, so all other file names beginning with a __.__ will match. To get the old behavior of ignoring file names beginning with a __.__ , make __.*__ one of the patterns in __GLOBIGNORE__ . The __dotglob__ option is disabled when __GLOBIGNORE__ is unset.
201
202
203 __Pattern Matching__
204
205 Any character that appears in a pattern, other than the special pattern characters described below, matches itself. The NUL character may not occur in a pattern. The special pattern characters must be quoted if they are to be matched literally.
206
207 The special pattern characters have the following meanings:
208
209
210
211
212 ;__*__ : Matches any string, including the null string.
213 ;__?__ : Matches any single character.
214 ;__[[...]__ : Matches any one of the enclosed characters. A pair of characters separated by a hyphen denotes a ''range expression''; any character that sorts between those two characters, inclusive, using the current locale's collating sequence and character set, is matched. If the first character following the __[[__ is a __!__ or a __^__ then any character not enclosed is matched. The sorting order of characters in range expressions is determined by the current locale and the value of the __LC_COLLATE__ shell variable, if set. A __-__ may be matched by including it as the first or last character in the set. A __]__ may be matched by including it as the first character in the set.
215
216
217 Within __[[__ and __]__ , ''character classes'' can be specified using the syntax __[[:__''class''__:]__, where ''class'' is one of the following classes defined in the POSIX.2 standard:
218 __alnum alpha ascii blank cntrl digit graph lower print punct space upper xdigit__.
219 A character class matches any character belonging to that class.
220
221 Within __[[__ and __]__ , an ''equivalence class'' can be specified using the syntax __[[=__''c''__=]__, which matches all characters with the same collation weight (as defined by the current locale) as the character ''c''.
222
223 Within __[[__ and __]__ , the syntax __[[.__''symbol''__.]__ matches the collating symbol ''symbol''.
224
225
226
227 If the __extglob__ shell option is enabled using the __shopt__ builtin, several extended pattern matching operators are recognized. In the following description, a ''pattern-list'' is a list of one or more patterns separated by a __|__. Composite patterns may be formed using one or more of the following sub-patterns:
228
229
230
231
232
233 ;__?(__''pattern-list''__)__ : Matches zero or one occurrence of the given patterns
234 ;__*(__''pattern-list''__)__ : Matches zero or more occurrences of the given patterns
235 ;__+(__''pattern-list''__)__ : Matches one or more occurrences of the given patterns
236 ;__@(__''pattern-list''__)__ : Matches exactly one of the given patterns
237 ;__!(__''pattern-list''__)__ : Matches anything except one of the given patterns
238
239
240 !Quote Removal
241
242
243 After the preceding expansions, all unquoted occurrences of the characters __\__ , __'__ , and " that did not result from one of the above expansions are removed.
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.