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

The simplist 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 +++ 
indirect expansion uses a ! prefix to parameter and the value of parameter is a variable used in the substitution.

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

With the  : colon if unset OR null
without the colon only if unset.

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


If parameter is set, it is substituted.

If parameter is null or
   parameter is unset and followed by : :
the the script name, line number and an expansion of error message No quotes needed
is written to standard error.
Exits with a return code of 1 (unless it is not interactive).
The default error message is "parameter null or not set" guess which one!.

> 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+

Provide default
${parameter[:]-default}

If parameter is set, the value of default is substituted.

If parameter is null or
   parameter is unset and followed by : :
the expansion of default is substituted,

function checkRange
max=${3:-99} #  max is set to $3, unless $3 is not provided or is null, then it is defaulted to 99.
    ^   ^ no space
if [ $max -gt 1000 ]; then echo max to big; return 1;fi
return 0}
> echo ${99-"Nintyninth Parameter Is UNSET"}
Nintyninth Parameter Is UNSET
> echo ${LINES:-30}
30
> echo :$LINES:
::

parameter is unchanged.

SET parameter to default for the remainder of the script:
${parameter[:]=default}



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

> echo ${LINES:=30}
30
> echo :$LINES:
:30:

Positional parameters (for example $2,… and special parameters may not be assigned in this way.

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

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

If i evaluates to a negative number, it is 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, indexing starts at 1!

${#parameter} The number of characters (of the expanded) value of parameter is substituted.
echo $1 +++ ${#1}
editing-mode +++ 12
123456789012 
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