man bash - EXPANSION
时间:2010-08-19 来源:xborant
EXPANSION
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 substitu-
tion, arithmetic expansion, word splitting, and pathname expansion.
The order of expansions is: brace expansion, tilde expansion, parame-
ter, variable and arithmetic expansion and command substitution (done
in a left-to-right fashion), word splitting, and pathname expansion.
On systems that can support it, there is an additional expansion avail-
able: process substitution.
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).
Brace Expansion
Brace expansion is a mechanism by which arbitrary strings may be gener-
ated. This mechanism is similar to pathname expansion, but the file-
names generated need not exist. Patterns to be brace expanded take the
form of an optional preamble, followed by either a series of comma-sep-
arated strings or a sequence expression between a pair of braces, fol-
lowed 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.
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'.
A sequence expression takes the form {x..y}, where x and y are either
integers or single characters. When integers are supplied, the expres-
sion expands to each number between x and y, inclusive. When charac-
ters are supplied, the expression expands to each character lexico-
graphically between x and y, inclusive. Note that both x and y must be
of the same type.
Brace expansion is performed before any other expansions, and any char-
acters 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.
A correctly-formed brace expansion must contain unquoted opening and
closing braces, and at least one unquoted comma or a valid sequence
expression. 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 expan-
sion, the string ${ is not considered eligible for brace expansion.
This construct is typically used as shorthand when the common prefix of
the strings to be generated is longer than in the above example:
mkdir /usr/local/src/bash/{old,new,dist,bugs}
or
chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
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 com-
mand (see SHELL BUILTIN COMMANDS below).
Tilde Expansion
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 direc-
tory of the user executing the shell is substituted instead. Other-
wise, the tilde-prefix is replaced with the home directory associated
with the specified login name.
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 char-
acters 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 argu-
ment. If the characters following the tilde in the tilde-prefix con-
sist of a number without a leading `+' or `-', `+' is assumed.
If the login name is invalid, or the tilde expansion fails, the word is
unchanged.
Each variable assignment is checked for unquoted tilde-prefixes immedi-
ately following a : or the first =. 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.
Parameter Expansion
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.
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 parameter
expansion.
${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.
If the first character of parameter is an exclamation point, a level of
variable indirection is introduced. Bash uses the value of the vari-
able 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 exceptions to this are the expansions
of ${!prefix*} and ${!name[@]} described below. The exclamation point
must immediately follow the left brace in order to introduce indirec-
tion.
In each of the cases below, word is subject to tilde expansion, parame-
ter 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 parame-
ter that is unset.
${parameter:-word}
Use Default Values. If parameter is unset or null, the expan-
sion of word is substituted. Otherwise, the value of parameter
is substituted.
${parameter:=word}
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 spe-
cial parameters may not be assigned to in this way.
${parameter:?word}
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.
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is
substituted, otherwise the expansion of word is substituted.
${parameter:offset}
${parameter:offset:length}
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 start-
ing at the character specified by offset. length and offset are
arithmetic expressions (see 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.
Arithmetic expressions starting with a - must be separated by
whitespace from the preceding : to be distinguished from the Use
Default Values expansion. 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]}. A
negative offset is taken relative to one greater than the maxi-
mum index of the specified array. Note that a negative offset
must be separated from the colon by at least one space to avoid
being confused with the :- expansion. Substring indexing is
zero-based unless the positional parameters are used, in which
case the indexing starts at 1.
${!prefix*}
${!prefix@}
Expands to the names of variables whose names begin with prefix,
separated by the first character of the IFS special variable.
${!name[@]}
${!name[*]}
If name is an array variable, expands to the list of array
indices (keys) assigned in name. If name is not an array,
expands to 0 if name is set and null otherwise. When @ is used
and the expansion appears within double quotes, each key expands
to a separate word.
${#parameter}
The length in characters of the value of parameter is substi-
tuted. 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.
${parameter#word}
${parameter##word}
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.
${parameter%word}
${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 pat-
tern (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.
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pat-
tern against its value is replaced with string. If Ipattern
begins with /, all matches of pattern are replaced with string.
Normally only the first match is replaced. 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 omit-
ted. 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 sub-
scripted with @ or *, the substitution operation is applied to
each member of the array in turn, and the expansion is the
resultant list.
Command Substitution
Command substitution allows the output of a command to replace the com-
mand name. There are two forms:
$(command)
or
`command`
Bash performs the expansion by executing command and replacing the com-
mand 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).
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 sub-
stitution. When using the $(command) form, all characters between the
parentheses make up the command; none are treated specially.
Command substitutions may be nested. To nest when using the backquoted
form, escape the inner backquotes with backslashes.
If the substitution appears within double quotes, word splitting and
pathname expansion are not performed on the results.
Arithmetic Expansion
Arithmetic expansion allows the evaluation of an arithmetic expression
and the substitution of the result. The format for arithmetic expan-
sion is:
$((expression))
The old format $[expression] is deprecated and will be removed in
upcoming versions of bash.
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 expansions may be
nested.
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.
Process Substitution
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 out-
put 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 pro-
vide input for list. If the <(list) form is used, the file passed as
an argument should be read to obtain the output of list.
When available, process substitution is performed simultaneously with
parameter and variable expansion, command substitution, and arithmetic
expansion.
Word Splitting
The shell scans the results of parameter expansion, command substitu-
tion, and arithmetic expansion that did not occur within double quotes
for word splitting.
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.
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.
Note that if no expansion occurs, no splitting is performed.
Pathname Expansion
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 dis-
abled, the word is left unchanged. If the nullglob option is set, and
no matches are found, the word is removed. If the failglob shell
option is set, and no matches are found, an error message is printed
and the command is not executed. If the shell option nocaseglob is
enabled, the match is performed without regard to the case of alpha-
betic characters. Note that when using range expressions like [a-z]
(see below), letters of the other case may be included, depending on
the setting of LC_COLLATE. When a pattern is used for pathname expan-
sion, the character ``.'' at the start of a name or immediately fol-
lowing a slash must be matched explicitly, unless the shell option dot-
glob 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, fail-
glob, and dotglob shell options.
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 when GLOBIGNORE is set and not null. However, setting GLOBIG-
NORE to a non-null value 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.
Pattern Matching
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. A backslash escapes the following character; the
escaping backslash is discarded when matching. The special pattern
characters must be quoted if they are to be matched literally.
The special pattern characters have the following meanings:
* Matches any string, including the null string.
? Matches any single character.
[...] Matches any one of the enclosed characters. A pair of charac-
ters separated by a hyphen denotes a range expression; any char-
acter 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 charac-
ter in the set. A ] may be matched by including it as the first
character in the set.
Within [ and ], character classes can be specified using the
syntax [:class:], where class is one of the following classes
defined in the POSIX standard:
alnum alpha ascii blank cntrl digit graph lower print punct
space upper word xdigit
A character class matches any character belonging to that class.
The word character class matches letters, digits, and the char-
acter _.
Within [ and ], an equivalence class can be specified using the
syntax [=c=], which matches all characters with the same colla-
tion weight (as defined by the current locale) as the character
c.
Within [ and ], the syntax [.symbol.] matches the collating sym-
bol symbol.
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 fol-
lowing sub-patterns:
?(pattern-list)
Matches zero or one occurrence of the given patterns
*(pattern-list)
Matches zero or more occurrences of the given patterns
+(pattern-list)
Matches one or more occurrences of the given patterns
@(pattern-list)
Matches one of the given patterns
!(pattern-list)
Matches anything except one of the given patterns
Quote Removal
After the preceding expansions, all unquoted occurrences of the charac-
ters \, ', and " that did not result from one of the above expansions
are removed.
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 substitu-
tion, arithmetic expansion, word splitting, and pathname expansion.
The order of expansions is: brace expansion, tilde expansion, parame-
ter, variable and arithmetic expansion and command substitution (done
in a left-to-right fashion), word splitting, and pathname expansion.
On systems that can support it, there is an additional expansion avail-
able: process substitution.
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).
Brace Expansion
Brace expansion is a mechanism by which arbitrary strings may be gener-
ated. This mechanism is similar to pathname expansion, but the file-
names generated need not exist. Patterns to be brace expanded take the
form of an optional preamble, followed by either a series of comma-sep-
arated strings or a sequence expression between a pair of braces, fol-
lowed 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.
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'.
A sequence expression takes the form {x..y}, where x and y are either
integers or single characters. When integers are supplied, the expres-
sion expands to each number between x and y, inclusive. When charac-
ters are supplied, the expression expands to each character lexico-
graphically between x and y, inclusive. Note that both x and y must be
of the same type.
Brace expansion is performed before any other expansions, and any char-
acters 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.
A correctly-formed brace expansion must contain unquoted opening and
closing braces, and at least one unquoted comma or a valid sequence
expression. 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 expan-
sion, the string ${ is not considered eligible for brace expansion.
This construct is typically used as shorthand when the common prefix of
the strings to be generated is longer than in the above example:
mkdir /usr/local/src/bash/{old,new,dist,bugs}
or
chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
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 com-
mand (see SHELL BUILTIN COMMANDS below).
Tilde Expansion
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 direc-
tory of the user executing the shell is substituted instead. Other-
wise, the tilde-prefix is replaced with the home directory associated
with the specified login name.
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 char-
acters 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 argu-
ment. If the characters following the tilde in the tilde-prefix con-
sist of a number without a leading `+' or `-', `+' is assumed.
If the login name is invalid, or the tilde expansion fails, the word is
unchanged.
Each variable assignment is checked for unquoted tilde-prefixes immedi-
ately following a : or the first =. 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.
Parameter Expansion
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.
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 parameter
expansion.
${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.
If the first character of parameter is an exclamation point, a level of
variable indirection is introduced. Bash uses the value of the vari-
able 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 exceptions to this are the expansions
of ${!prefix*} and ${!name[@]} described below. The exclamation point
must immediately follow the left brace in order to introduce indirec-
tion.
In each of the cases below, word is subject to tilde expansion, parame-
ter 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 parame-
ter that is unset.
${parameter:-word}
Use Default Values. If parameter is unset or null, the expan-
sion of word is substituted. Otherwise, the value of parameter
is substituted.
${parameter:=word}
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 spe-
cial parameters may not be assigned to in this way.
${parameter:?word}
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.
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is
substituted, otherwise the expansion of word is substituted.
${parameter:offset}
${parameter:offset:length}
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 start-
ing at the character specified by offset. length and offset are
arithmetic expressions (see 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.
Arithmetic expressions starting with a - must be separated by
whitespace from the preceding : to be distinguished from the Use
Default Values expansion. 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]}. A
negative offset is taken relative to one greater than the maxi-
mum index of the specified array. Note that a negative offset
must be separated from the colon by at least one space to avoid
being confused with the :- expansion. Substring indexing is
zero-based unless the positional parameters are used, in which
case the indexing starts at 1.
${!prefix*}
${!prefix@}
Expands to the names of variables whose names begin with prefix,
separated by the first character of the IFS special variable.
${!name[@]}
${!name[*]}
If name is an array variable, expands to the list of array
indices (keys) assigned in name. If name is not an array,
expands to 0 if name is set and null otherwise. When @ is used
and the expansion appears within double quotes, each key expands
to a separate word.
${#parameter}
The length in characters of the value of parameter is substi-
tuted. 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.
${parameter#word}
${parameter##word}
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.
${parameter%word}
${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 pat-
tern (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.
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pat-
tern against its value is replaced with string. If Ipattern
begins with /, all matches of pattern are replaced with string.
Normally only the first match is replaced. 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 omit-
ted. 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 sub-
scripted with @ or *, the substitution operation is applied to
each member of the array in turn, and the expansion is the
resultant list.
Command Substitution
Command substitution allows the output of a command to replace the com-
mand name. There are two forms:
$(command)
or
`command`
Bash performs the expansion by executing command and replacing the com-
mand 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).
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 sub-
stitution. When using the $(command) form, all characters between the
parentheses make up the command; none are treated specially.
Command substitutions may be nested. To nest when using the backquoted
form, escape the inner backquotes with backslashes.
If the substitution appears within double quotes, word splitting and
pathname expansion are not performed on the results.
Arithmetic Expansion
Arithmetic expansion allows the evaluation of an arithmetic expression
and the substitution of the result. The format for arithmetic expan-
sion is:
$((expression))
The old format $[expression] is deprecated and will be removed in
upcoming versions of bash.
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 expansions may be
nested.
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.
Process Substitution
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 out-
put 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 pro-
vide input for list. If the <(list) form is used, the file passed as
an argument should be read to obtain the output of list.
When available, process substitution is performed simultaneously with
parameter and variable expansion, command substitution, and arithmetic
expansion.
Word Splitting
The shell scans the results of parameter expansion, command substitu-
tion, and arithmetic expansion that did not occur within double quotes
for word splitting.
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.
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.
Note that if no expansion occurs, no splitting is performed.
Pathname Expansion
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 dis-
abled, the word is left unchanged. If the nullglob option is set, and
no matches are found, the word is removed. If the failglob shell
option is set, and no matches are found, an error message is printed
and the command is not executed. If the shell option nocaseglob is
enabled, the match is performed without regard to the case of alpha-
betic characters. Note that when using range expressions like [a-z]
(see below), letters of the other case may be included, depending on
the setting of LC_COLLATE. When a pattern is used for pathname expan-
sion, the character ``.'' at the start of a name or immediately fol-
lowing a slash must be matched explicitly, unless the shell option dot-
glob 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, fail-
glob, and dotglob shell options.
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 when GLOBIGNORE is set and not null. However, setting GLOBIG-
NORE to a non-null value 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.
Pattern Matching
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. A backslash escapes the following character; the
escaping backslash is discarded when matching. The special pattern
characters must be quoted if they are to be matched literally.
The special pattern characters have the following meanings:
* Matches any string, including the null string.
? Matches any single character.
[...] Matches any one of the enclosed characters. A pair of charac-
ters separated by a hyphen denotes a range expression; any char-
acter 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 charac-
ter in the set. A ] may be matched by including it as the first
character in the set.
Within [ and ], character classes can be specified using the
syntax [:class:], where class is one of the following classes
defined in the POSIX standard:
alnum alpha ascii blank cntrl digit graph lower print punct
space upper word xdigit
A character class matches any character belonging to that class.
The word character class matches letters, digits, and the char-
acter _.
Within [ and ], an equivalence class can be specified using the
syntax [=c=], which matches all characters with the same colla-
tion weight (as defined by the current locale) as the character
c.
Within [ and ], the syntax [.symbol.] matches the collating sym-
bol symbol.
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 fol-
lowing sub-patterns:
?(pattern-list)
Matches zero or one occurrence of the given patterns
*(pattern-list)
Matches zero or more occurrences of the given patterns
+(pattern-list)
Matches one or more occurrences of the given patterns
@(pattern-list)
Matches one of the given patterns
!(pattern-list)
Matches anything except one of the given patterns
Quote Removal
After the preceding expansions, all unquoted occurrences of the charac-
ters \, ', and " that did not result from one of the above expansions
are removed.
相关阅读 更多 +