BASH Parameter Expansions
Content
Parameter Expansions
Reference | Set and Not Null | Set but Null | Unset |
---|---|---|---|
${parameter-word} |
substitute parameter | substitute null | substitute word |
${parameter:-word} |
substitute parameter | substitute word | substitute word |
${parameter+word} |
substitute word | substitute word | substitute null |
${parameter:+word} |
substitute word | substitute null | substitute null |
${parameter=word} |
substitute parameter | substitute null | assign word |
${parameter:=word} |
substitute parameter | assign word | assign word |
${parameter?word} |
substitute parameter | error, exit | error, exit |
${parameter:?word} |
substitute parameter | substitute null | error, exit |
Substring
${parameter#pattern}
Expand to parameter with shortest matching pattern from the beginning deleted.
${parameter##pattern}
Expand to parameter with longest matching pattern from the beginning deleted.
${parameter%pattern}
Expand to parameter with shortest matching pattern from the end deleted.
${parameter%%pattern}
Expand to parameter with longest matching pattern from the end deleted.
Reference to Variables
${!word}
Expand to variable ${$word}
if it's defined, error otherwise. Note that word is non-recursive; references like ${!operation_${codename}}
will fail.
Example:
$vairable=value
$word=variable
$echo $word
variable $echo $vairable
value $echo ${!word}
value $codename=blueline
$operation_blueline=success!
$echo $operation_blueline
success! $echo ${!operation_$codename}
bash: ${!operation_$codename}: bad substitution