previous(Tilde substitution), next(command substitution), table of contents.


Parameter Expansion

(Perhaps "Parameter Editing" would be a better phrase, ed)

The basic form of parameter expansion is : ${parameter}.

The value of parameter is substituted.
Braces are optional when parameter is a single digit positional parameter.

echo $1 +++ $1zzz +++ ${1} +++ ${1}zzz
editing-mode +++ editing-modezzz +++ editing-mode +++ editing-modezzz
When braces are used, the matching ending brace is the first one not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.

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.

In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
${parameter-word}



${parameter:-word}

If parameter is unset, the expansion of word is substituted.
Otherwise, the value of parameter is substituted.

echo ${99-nintyninthParameterIsUNSET}
nintyninthParameterIsUNSET
If parameter is unset or null
${parameter=word}


${parameter:=word}

If parameter is unset , the expansion of word is assigned to parameter The value of parameter is then substituted.

If parameter is unset or null
Positional parameters and special parameters may not be assigned to in this way.

${parameter?word}



${parameter:?word}

If parameter is 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 exits (unless it is not interactive).
Otherwise, the value of parameter is substituted.

If parameter is unset or null

${parameter:+word}

${parameter:+word}

If parameter is null or unset, nothing is substituted,
otherwise the expansion of word is substituted.
${parameter:offset:length}
${parameter:offset}
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 arithemtic expressions.

This is a Substring Expansion.
length must evaluate to zero or greater.

  • 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.
${#parameter} The length in characters of the expanded value of parameter is substituted.
echo $1 +++ ${#1}
editing-mode +++ 12
  • For ${*} 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.

word is expanded to produce a pattern as in filename expansion.
${parameter#word}
${parameter##word}
  • If that pattern matches the beginning of the expanded value of parameter, the result is the expanded value of parameter with the shortest ( # ) or longest ( ## ) matching pattern deleted.
  • For ${*% … or ${@% …, the pattern removal is applied to each positional parameter and the result is the 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}
  • If that pattern matches the ending
pattern is expanded to produce a pattern as in filename expansion
${parameter/[#|%]pattern/string}
${parameter//[#|%]pattern/string}
Parameter is expanded and the longest match of pattern against that value is replaced with string.
  Only the first (/) or all (//) match is replaced.
For #pattern match beginning of string, for %pattern match end of string.
${parameter/[#|%]pattern} ${parameter//[#|%]pattern}
  • If string is null, matches of pattern are deleted. The / following pattern is optional.
  • For ${*# … or ${@# …,
            ${*% … or ${@%
      the substitution is applied to positional parameters and the result is the list.
  • If parameter is an array variable subscripted with @ or *, the substitution is applied to each member of the array in turn. Result is that list.
  • Command Substitution