OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Expansions To Manage Empty Variables

Several parameter expansions are intended to deal with nonexistent and empty variables. These expansions are handy for handling missing positional parameters and assigning de- fault values to parameters.

${parameter:-word}

If parameter is unset (i.e., does not exist) or is empty, this expansion results in the value of word. If parameter is not empty, the expansion results in the value of parameter.



[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo ${foo:-"substitute value if unset"}

substitute value if unset [me@linuxbox ~]$ echo $foo


[me@linuxbox ~]$ foo=bar

[me@linuxbox ~]$ echo ${foo:-"substitute value if unset"}

bar

[me@linuxbox ~]$ echo $foo

bar

[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo ${foo:-"substitute value if unset"}

substitute value if unset [me@linuxbox ~]$ echo $foo


[me@linuxbox ~]$ foo=bar

[me@linuxbox ~]$ echo ${foo:-"substitute value if unset"}

bar

[me@linuxbox ~]$ echo $foo

bar


${parameter:=word}

If parameter is unset or empty, this expansion results in the value of word. In addition, the value of word is assigned to parameter. If parameter is not empty, the expansion re- sults in the value of parameter.


[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo ${foo:="default value if unset"}

default value if unset [me@linuxbox ~]$ echo $foo default value if unset [me@linuxbox ~]$ foo=bar

[me@linuxbox ~]$ echo ${foo:="default value if unset"}

bar

[me@linuxbox ~]$ echo $foo

bar

[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo ${foo:="default value if unset"}

default value if unset [me@linuxbox ~]$ echo $foo default value if unset [me@linuxbox ~]$ foo=bar

[me@linuxbox ~]$ echo ${foo:="default value if unset"}

bar

[me@linuxbox ~]$ echo $foo

bar


image

Note: Positional and other special parameters cannot be assigned this way.


image


${parameter:?word}

If parameter is unset or empty, this expansion causes the script to exit with an error, and the contents of word are sent to standard error. If parameter is not empty, the expansion results in the value of parameter.



[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo ${foo:?"parameter is empty"}

bash: foo: parameter is empty [me@linuxbox ~]$ echo $?

1

[me@linuxbox ~]$ foo=bar

[me@linuxbox ~]$ echo ${foo:?"parameter is empty"}

bar

[me@linuxbox ~]$ echo $?

0

[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo ${foo:?"parameter is empty"}

bash: foo: parameter is empty [me@linuxbox ~]$ echo $?

1

[me@linuxbox ~]$ foo=bar

[me@linuxbox ~]$ echo ${foo:?"parameter is empty"}

bar

[me@linuxbox ~]$ echo $?

0


${parameter:+word}

If parameter is unset or empty, the expansion results in nothing. If parameter is not empty, the value of word is substituted for parameter; however, the value of parameter is not changed.



[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo ${foo:+"substitute value if set"}


[me@linuxbox ~]$ foo=bar

[me@linuxbox ~]$ echo ${foo:+"substitute value if set"}

[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo ${foo:+"substitute value if set"}


[me@linuxbox ~]$ foo=bar

[me@linuxbox ~]$ echo ${foo:+"substitute value if set"}


substitute value if set

substitute value if set


Top OS Cloud Computing at OnWorks: