4.1. Variable Substitution(双引号,一行赋多值,未初始化变量)�

1、双引号内的变量可以保留变量值当中的超过一个空格的空格:

(abs-guide-3.7/HTML/variables.html#VARSUBN)

Example 4-1. Variable assignment and substitution


  33 hello="A B  C   D"

  34 echo $hello   # A B C D

  35 echo "$hello" # A B  C   D

  36 # As you see, echo $hello   and   echo "$hello"   give different results.

  37 #                                      ^      ^

  38 # Quoting a variable preserves whitespace.

2、可以在一行中对多个变量赋值,但是在有可能引起版本兼容性问题:

(abs-guide-3.7/HTML/variables.html#VARSUBN)

Example 4-1. Variable assignment and substitution


  57 #  It is permissible to set multiple variables on the same line,

  58 #+ if separated by white space.

  59 #  Caution, this may reduce legibility, and may not be portable.

  60

  61 var1=21  var2=22  var3=$V3

  62 echo

  63 echo "var1=$var1   var2=$var2   var3=$var3"

  64

  65 # May cause problems with older versions of "sh".

3、在没有初始化的变量上进行算术运算不是非法的,当作是"0",但是在有可能引起版本兼容性问题:

(abs-guide-3.7/HTML/variables.html#VARSUBN)


An uninitialized variable has a "null" value - no assigned value at all (not zero!). Using a variable before assigning a value to it will usually cause problems.

It is nevertheless possible to perform arithmetic operations on an uninitialized variable.

   1 echo "$uninitialized"                                # (blank line)

   2 let "uninitialized += 5"                             # Add 5 to it.

   3 echo "$uninitialized"                                # 5

   4

   5 #  Conclusion:

   6 #  An uninitialized variable has no value, however

   7 #+ it acts as if it were 0 in an arithmetic operation.

   8 #  This is undocumented (and probably non-portable) behavior.