Variable substitution problem

HI

i was studying about variable substitution. below are info which was given in a online tutorial.

 
${parameter:-word}---> If parameter is null or unset, word is substituted for parameter.
          The value of parameter does not change.
${parameter:=word}---> If parameter is null or unset, parameter is set to the value of word.
${parameter:?message}--? If parameter is null or unset, message is printed to standard error. 
           This checks that variables are set correctly.
${parameter:+word}---> If parameter is set, word is substituted for parameter.
          The value of parameter does not change.

in order to understand its functinality i write a small script mentioned as below.
here i am not able to understand the use of

${parameter:?message} 

and

${parameter:+word}

and why it is taking initial value of parameter which is "two" in last 2 cases.

 
$ cat subs
#!/bin/sh
#// ${parameter:-word}
echo ${parameter:-"this is one"}
echo "value of parameter ${parameter}"
echo next 
#//${parameter:=word}
echo ${parameter:="this is two"}
echo "value of paramer is ${parameter}"
echo next
#//${parameter:+word}
echo ${parameter:+"this is three"}
echo "value of parameter is ${parameter}"
echo next
echo ${parameter:? "this is three"}
echo "value of parameter is ${parameter}"
$ 

O/P

 
this is one
value of parameter 
next
this is two
value of paramer is this is two
next
this is three
value of parameter is this is two
next
this is two
value of parameter is this is two

${var?message} will display the "message" when "var" is not set.

[root@it misc]# echo ${aVarNotSet?"aVarNotSet is not set"}
-bash: aVarNotSet: aVarNotSet is not set

${var+message} will substitute "message" if "var" is set, i.e., it will say that "var" contains "message" instead of the value that is stored in "var"

[root@it misc]# aVarNotSet="aVarNotSet is set"
[root@it misc]# echo ${aVarNotSet+"aVarNotSet is not set"}
aVarNotSet is not set
[root@it misc]# echo ${aVarNotSet}
aVarNotSet is set

As to your question, in the second case you are assigning the value to "parameter". So, now the "parameter" contains "this is two", and thus it is set.

In the third case it do not assign any value to "parameter", just substitute "this is three" instead of "this is two" and thus "parameter" remains unchanged.
In the last case it just checks is "parameter" is set, if not, prints the error message, "this is three". Since in this case "parameter" is set, you will not see the error message.

Hi chaco193,

here i am not able to understatnd the practical use of variable substitution.

  1. where and in which type of condition we can use these type ?

  2. under which circumstances such type of substitution are helpful ?

  3. why we use such type of substitution when we can use simple way like

x=5

may be my question looks to you silly but if you explain this to me with the help of some example ,this will really help me .

scriptor

There is too many instances in creating scripts when we don't know the content of a variable. i.e. a variable gets set by another computation, or input from another process or user. Not always the result is acceptable or can be assumed.

Using those facilities, ensures that the minimal or acceptable defaults are available, perhaps to recover, instead of letting our script just fail, or fail us with bad results.