Info Node: (bashref.info)Shell Parameter Expansion
(bashref.info)Shell Parameter Expansion
Shell 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.
The basic form of parameter expansion is ${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 that 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
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.
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. Put another way, if the colon is included,
the operator tests for both existence and that the value is not null;
if the colon is omitted, the operator tests only for existence.
`${PARAMETER:-WORD}'
If PARAMETER is unset or null, the expansion of WORD is
substituted. Otherwise, the value of PARAMETER is substituted.
`${PARAMETER:=WORD}'
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.
`${PARAMETER:?WORD}'
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}'
If PARAMETER is null or unset, nothing is substituted, otherwise
the expansion of WORD is substituted.
`${PARAMETER:OFFSET}'
`${PARAMETER:OFFSET:LENGTH}'
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 (Note:Shell
Arithmetic). This is referred to as Substring Expansion.
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.
`${!PREFIX*}'
Expands to the names of variables whose names begin with PREFIX,
separated by the first character of the `IFS' special variable.
`${#PARAMETER}'
The length in characters of the expanded 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.
`${PARAMETER#WORD}'
`${PARAMETER##WORD}'
The WORD is expanded to produce a pattern just as in filename
expansion (Note:Filename Expansion). If the pattern matches
the beginning 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.
`${PARAMETER%WORD}'
`${PARAMETER%%WORD}'
The WORD is expanded to produce a pattern just as in filename
expansion. If the pattern matches a trailing portion of the
expanded value of PARAMETER, then the result of the expansion is
the 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/PATTERN/STRING}'
`${PARAMETER//PATTERN/STRING}'
The PATTERN is expanded to produce a pattern just as in filename
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.