Understanding Variable Expansion
Variable expansion, which is also called parameter expansion, allows the shell to test or modify values of a variable to be used in a script, using special modifiers enclosed in curly braces and preceded by a $
(${variable}
). If this variable is not set in bash
it will be expanded to a null string. The best way to begin is to show you a few simple examples.
Substituting a Value for an Unset Variable
First, I’ll define the cat
variable with the name of my 16-year old gray kitty. Then, I’ll perform a test to see if cat
really has a set value, like this:
[donnie@fedora ~]$ cat=Vicky
[donnie@fedora ~]$ echo ${cat-"This cat variable is not set."}
Vicky
[donnie@fedora ~]$
Next, I’ll unset the value of cat
, and perform the test again. Watch what happens:
[donnie@fedora ~]$ unset cat
[donnie@fedora ~]$ echo ${cat-"This cat variable is not set."}
This cat variable is not set.
[donnie@fedora ~]$
...