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
If
> 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 function checkRange max=${3:-99} # max > echo ${99-"Nintyninth Parameter Is UNSET"} Nintyninth Parameter Is UNSET > echo ${LINES:-30} 30 > echo :$LINES: ::
|
SET parameter to default for the remainder of the script:${parameter[:]=default}
|
If
> echo ${LINES:=30} 30 > echo :$LINES: :30:
Positional parameters (for example
|
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 i th character for n characters.
If
If
If |
${#parameter}
| The number of characters (of the expanded) value of parameter is substituted.
echo $1 +++ ${#1} editing-mode +++ 12 123456789012For ${*} or ${@} , the value substituted is the number of positional parameters.
If
|
word is expanded to produce a pattern as in filename expansion.
| |
---|---|
${parameter#word} ${parameter##word}
|
|
${parameter%word} ${parameter%%word}
|
|
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}
|
string is null, matches of pattern are deleted. The / following pattern is optional.
${*# … or ${@# …, ${*% … or ${@% … the substitution is applied to positional parameters and the result is the list. parameter is an array variable subscripted with @ or * , the substitution
is applied to each member of the array in turn. Result is that list.
|