Bash Commands - $ # Variable Substitution

$

Let us carefully distinguish between the name of a variable and its value. If variable1 is the name
of a variable, then $variable1 is a reference to its value, the data item it contains.

bash$ variable1=23
bash$ echo variable1
variable1


bash$ echo $variable1
23

 

Example 4-1. Variable assignment and substitution

#!/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
# Also a variable reference, as above.

 

echo
hello="A B  C   D"
echo $hello   # A B C D
echo "$hello" # A B  C   D
# As you see, echo $hello   and   echo "$hello"   give different results.
# Why?
# =======================================
# 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"
# 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 older 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"
# It still has a null value.
echo
exit 0




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值