变量的名称是其值(它所保存的数据)的占位符。引用(检索)其值称为变量替换。
让我们仔细区分变量的名称和它的值。如果variable1是变量的名称,那么$variable1是对它的值(它包含的数据项)的引用。[1]
bash$ variable1=23 bash$ echo variable1 variable1 bash$ echo $variable1 23 |
变量仅在“裸露”出现时——没有$前缀——才是在声明或赋值时,当取消设置时,当导出时,在双括号 (( ... ))内的算术表达式中,或者在代表信号的变量的特殊情况下(参见示例 32-5)。赋值可以使用=(如var1=27中所示),在 read 语句中,以及在循环的开头(for var2 in 1 2 3).
将引用的值用双引号 (" ... ") 括起来不会干扰变量替换。这被称为部分引用,有时被称为“弱引用”。使用单引号 (' ... ') 会导致变量名按字面意义使用,并且不会发生替换。这是完全引用,有时被称为“强引用”。有关详细讨论,请参见第 5 章。
请注意$variable实际上是${variable}的简化形式。在$variable语法导致错误的情况下,较长的形式可能会起作用(请参见下面的10.2 节)。
示例 4-1. 变量赋值和替换
#!/bin/bash
# ex9.sh
# Variables: assignment and substitution
a=375
hello=$a
# ^ ^
#-------------------------------------------------------------------------
# No space permitted on either side of = sign when initializing variables.
# What happens if there is a space?
# "VARIABLE =value"
# ^
#% Script tries to run "VARIABLE" command with one argument, "=value".
# "VARIABLE= value"
# ^
#% Script tries to run "value" command with
#+ the environmental variable "VARIABLE" set to "".
#-------------------------------------------------------------------------
echo hello # hello
# Not a variable reference, just the string "hello" ...
echo $hello # 375
# ^ This *is* a variable reference.
echo ${hello} # 375
# Likewise a variable reference, as above.
# Quoting . . .
echo "$hello" # 375
echo "${hello}" # 375
echo
hello="A B C D"
echo $hello # A B C D
echo "$hello" # A B C D
# As we see, echo $hello and echo "$hello" give different results.
# =======================================
# Quoting a variable preserves whitespace.
# =======================================
echo
echo '$hello' # $hello
# ^ ^
# Variable referencing disabled (escaped) by single quotes,
#+ which causes the "$" to be interpreted literally.
# Notice the effect of different types of quoting.
hello= # Setting it to a null value.
echo "\$hello (null value) = $hello" # $hello (null value) =
# Note that setting a variable to a null value is not the same as
#+ unsetting it, although the end result is the same (see below).
# --------------------------------------------------------------
# It is permissible to set multiple variables on the same line,
#+ if separated by white space.
# Caution, this may reduce legibility, and may not be portable.
var1=21 var2=22 var3=$V3
echo
echo "var1=$var1 var2=$var2 var3=$var3"
# May cause problems with legacy versions of "sh" . . .
# --------------------------------------------------------------
echo; echo
numbers="one two three"
# ^ ^
other_numbers="1 2 3"
# ^ ^
# If there is whitespace embedded within a variable,
#+ then quotes are necessary.
# other_numbers=1 2 3 # Gives an error message.
echo "numbers = $numbers"
echo "other_numbers = $other_numbers" # other_numbers = 1 2 3
# Escaping the whitespace also works.
mixed_bag=2\ ---\ Whatever
# ^ ^ Space after escape (\).
echo "$mixed_bag" # 2 --- Whatever
echo; echo
echo "uninitialized_variable = $uninitialized_variable"
# Uninitialized variable has null value (no value at all!).
uninitialized_variable= # Declaring, but not initializing it --
#+ same as setting it to a null value, as above.
echo "uninitialized_variable = $uninitialized_variable"
# It still has a null value.
uninitialized_variable=23 # Set it.
unset uninitialized_variable # Unset it.
echo "uninitialized_variable = $uninitialized_variable"
# uninitialized_variable =
# It still has a null value.
echo
exit 0 |
![]() | 未初始化的变量具有“空”值——根本没有赋值(不是零!)。
在为变量赋值之前使用变量可能会导致问题。然而,对未初始化的变量执行算术运算是可能的。
|
| [1] | 从技术上讲,变量的名称被称为左值,意思是它出现在赋值语句的左侧,如VARIABLE=23中所示。变量的值是一个右值,意思是它出现在赋值语句的右侧,如VAR2=$VARIABLE. |