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


Parameter Expansion

(Perhaps "Parameter evaluating" would be a better phrase, ed)
Especially useful within functions or in script files.

The basic form is : ${parameter}. The value of parameter is substituted.
Braces are optional when parameter is a single digit positional parameter.

> echo $1 +++ ${1} +++
editing-mode +++ editing-mode +++
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 ! prefixes parameter the value of parameter is a variable used in the substitution. 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.

require a value:
${parameter:?[error message]}


${parameter?[error message]}

If parameter is unset or null , the script name, line number and an expansion of error message
is written to standard error and the shell exits with a return code of 1 (unless it is not interactive).
The default error message is "parameter null or not set" .

If parameter is unset …

Otherwise, the value of parameter is substituted.

> cat 00.sh
echo Size is ${SIZE?": must be set"}
echo positionalParameter1 is ${1? ":posPar1 is required"}
echo +end of script+

> ./00.sh
./00.sh: line 1: size: : must be set
echo $?
1

export SIZE=1234
> ./00.sh
Size is 1234
./00.sh: line 2: 1: :posPar1 is required

> ./00.sh 88
Size is 1234
positionalParameter1 is 88
+end of script+

SET a default value for the remainder of the script:
${parameter:=word}


${parameter=word}


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

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

> echo ${LINES:=30}
30
> echo :$LINES:
:30:
To provide a default value:
${parameter-word}

${parameter:-word}

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

If parameter is unset or null

> echo ${99-"Nintyninth Parameter Is UNSET"}
Nintyninth Parameter Is UNSET
> echo ${LINES:-30}
30
> echo :$LINES:
::
To OVERRIDE a value:
${parameter:+word}

${parameter:+word}

If parameter is null or unset, nothing is substituted,
otherwise the expansion of word is substituted.
To use a substring of the value:
${parameter:i[:n]}
substitutes the substring of parameter, starting with the ith character for n characters.

n and i are arithemtic expressions.
If n is omitted, ith.
n must evaluate to zero or greater. Substring indexing is zero-based.

If i 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 an array name indexed by @ or *, the result is the n members of the array beginning with ${parameter[i]}.

If parameter is @, the result is n positional parameters beginning at i.
In this case indexing starts at 1.

${#parameter} The number of 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