Using Parameter Expansion in POSIX Shell
In POSIX shell scripting, we can use parameter expansion to reference a variable
message="hello"
echo ${message}
The usefulness of this syntax is in its variations that handle variables when null/not null:
Form | Meaning |
---|---|
${var:-word} | use word if var unset/null |
${var:=word} | use word and set var to word if var unset/null |
${var:+word} | use word if var set and not null |
${var:?word} | exit with error and print word if var unset/null |
The Open Group Base Specifications Issue 7 (2.6.2 Parameter Expansion)