Bash - Parameter Expansion ${

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

Parameter expansion are patterns applied to the parameters that have differents results.

Same as Language - Variable substitution ??

See also: Bash - Colon (:) Builtin command

Advertising

3 - Pattern / Syntax's

3.1 - ${parameter}

The value of parameter is substituted

${parameter}

where:

  • the braces are optional. They served
    • to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name. 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.
    • 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.

3.1.1 - indirect expansion

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.

Advertising

3.2 - Default Value Syntax

3.2.1 - ${parameter:-word} - Not assigned

Use Default Values.

  • If parameter is unset or null, the expansion of word is substituted.
echo ${UNKNOWN:-default}
default
  • but not set
echo ${UNKNOWN}

3.2.2 - ${parameter:=word} - Assigned

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
Advertising

3.3 - Not Null

3.3.1 - ${parameter:?word} - Display error if null

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.

  • Without word, we get a standard message
echo ${UNKNOWN?}
bash: UNKNOWN: parameter null or not set
  • With word:
echo ${UNKNOWN?The unknown variable is not set, sorry}
bash: UNKNOWN: The unknown variable is not set, sorry

3.3.2 - ${parameter:+word} - Alternate Value if not null

Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

  • when the parameter is unset, their is no expansion
unset UNKNOWN
echo ${UNKNOWN:+default}
    # nothing is returned
  • when the parameter is set
UNKNOWN=unknown
echo ${UNKNOWN:+default}
default

3.4 - Slicing

${parameter:offset}
${parameter:offset:length}

The parameter can be:

And:

  • length and offset are arithmetic expressions
  • 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.
  • A negative offset must be separated from the colon by at least one space to avoid being confused with the :- expansion.
  • If length is omitted, expands to the substring of parameter starting at the character or array position specified by offset.

Substring indexing is zero-based unless the positional parameters array is used, in which case the indexing starts at 1.

3.4.1 - Substring

If parameter is a string. The Substring Expansion expands to up to length characters of parameter starting at the character specified by offset

Example:

  • offset positive: 2
UNKNOWN=unknown
echo ${UNKNOWN:2}
known
  • offset negative: -5
echo ${UNKNOWN: -5} # don't forget the space between the colon and the negative character 
known
  • offset positive + length
echo ${UNKNOWN:2:3} 
kno

3.4.2 - Array

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
  • we can suppress the first element
echo ${BASH_VERSINFO[@]:1}
3 46 1 release x86_64-pc-linux-gnu
  • we can slice it to get the element 1 to 4
echo ${BASH_VERSINFO[@]:1:4}
3 46 1 release
  • we can also use a negative offset to get the last element
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

3.5 - Prefix

${!prefix*}
${[email protected]}

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*}
  • output:
Prefix1 Prefix2

3.6 - Array Indices

${!name[@]}
${!name[*]}

If name is:

  • an array variable, expands to the list of array indices (keys) assigned in name.
  • 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.

Example:

  • Array
name[0]=value0
name[1]=value1
echo ${!name[*]}
  • Output
0 1

3.7 - Length

${#parameter}

If parameter is:

  • a string: The length in characters of the value of parameter is substituted.
  • an array name subscripted by * or @, the value substituted is the number of elements in the array.
  • * or @, the value substituted is the number of positional parameters.
MY_VARIABLE=123
echo ${#MY_VARIABLE}
3

3.8 - Removal operation

3.8.1 - Beginning

${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:

  • 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.
  • 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.

Example:

MY_VARIABLE=123
echo ${MY_VARIABLE#1}
23

3.8.2 - Trailing

${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 pattern (the % case)
  • or the longest matching pattern (the %% case) deleted.

If parameter is:

3.9 - Pattern Longest match Replace

${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.

4 - Example

  • arithmetic expansion: $((EXPR))
echo One plus One is $((1+1))
echo One plus One is 2

5 - Command

You can expand parameter with the : command.

6 - Documentation / Reference