Parameter expansion are patterns applied to the parameters that have different results.
Same as Language - Variable substitution ??
See also: Bash - Colon (:) Builtin command
The value of parameter is substituted
${parameter}
where:
If the first character of parameter is an exclamation point, a level of variable indirection is introduced. The exclamation point must immediately follow the left brace in order to introduce indirection.
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.
The exceptions to this are the expansions of !prefix* and !name[@].
In each of the cases below, word is subject to:
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.
Use Default Values.
echo ${UNKNOWN:-default}
default
echo ${UNKNOWN}
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.
If parameter is unset or null, the expansion of word is substituted.
echo ${UNKNOWN:=default}
default
and set
echo ${UNKNOWN}
default
Display Error given by word if parameter is Null or Unset. If the shell is not interactive, the script exits. Otherwise, the value of parameter is substituted.
echo ${UNKNOWN?}
bash: UNKNOWN: parameter null or not set
echo ${UNKNOWN?The unknown variable is not set, sorry}
bash: UNKNOWN: The unknown variable is not set, sorry
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
unset UNKNOWN
echo ${UNKNOWN:+default}
# nothing is returned
UNKNOWN=unknown
echo ${UNKNOWN:+default}
default
${parameter:offset}
${parameter:offset:length}
The parameter can be:
And:
Substring indexing is zero-based unless the positional parameters array is used, in which case the indexing starts at 1.
If parameter is a string. The Substring Expansion expands to up to length characters of parameter starting at the character specified by offset
Example:
UNKNOWN=unknown
echo ${UNKNOWN:2}
known
echo ${UNKNOWN: -5} # don't forget the space between the colon and the negative character
known
echo ${UNKNOWN:2:3}
kno
If parameter is an array name indexed by @ or *, the result is an array with its length defined by offset and length. A negative offset is taken relative to one greater than the maximum index of the specified array.
Example with the array version, the array contains the following values
echo ${BASH_VERSINFO[@]}
4 3 46 1 release x86_64-pc-linux-gnu
echo ${BASH_VERSINFO[@]:1}
3 46 1 release x86_64-pc-linux-gnu
echo ${BASH_VERSINFO[@]:1:4}
3 46 1 release
echo ${BASH_VERSINFO[@]: -1}
x86_64-pc-linux-gnu
The positional parameter array @ may also be used. Let op, the indexing starts at 1.
echo ${@: -1} # to get the last argument
${!prefix*}
${!prefix@}
Expands to the names of variables whose names begin with prefix, separated by the first character of the IFS special variable.
Example:
Prefix1=Prefix1Value
Prefix2=Prefix2Value
echo ${!Prefix*}
Prefix1 Prefix2
${!name[@]}
${!name[*]}
If name is:
When @ is used and the expansion appears within double quotes, each key expands to a separate word.
Example:
name[0]=value0
name[1]=value1
echo ${!name[*]}
0 1
${#parameter}
If parameter is:
MY_VARIABLE=123
echo ${#MY_VARIABLE}
3
${parameter#word}
${parameter##word}
If parameter is a string. 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:
If parameter is:
Example:
MY_VARIABLE=123
echo ${MY_VARIABLE#1}
23
${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:
If parameter is:
${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. If pattern 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.
You can expand parameter with the : command.