美文网首页
Shell(五)

Shell(五)

作者: StarShift | 来源:发表于2016-11-18 14:50 被阅读11次

shell 的变量

#!/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

对比区别:

newer@ubuntu:~/script$ a=`ls -l`;echo $a
total 20 -rwxr-xr-x 1 root root 200 Nov 17 17:02 1.sh -rwxrwxr-x 1 newer newer 26 Nov 17 21:13 2.sh -rwxr-xr-x 1 root root 182 Nov 15 05:35 bianliang.sh -rwxr-xr-x 1 root root 112 Nov 14 17:12 case.sh -rwxr-xr-x 1 root root 2093 Nov 15 05:50 clean.sh
newer@ubuntu:~/script$ a=`echo hello`;echo "$a"
hello
newer@ubuntu:~/script$ a=`ls -l`;echo "$a"
total 20
-rwxr-xr-x 1 root  root   200 Nov 17 17:02 1.sh
-rwxrwxr-x 1 newer newer   26 Nov 17 21:13 2.sh
-rwxr-xr-x 1 root  root   182 Nov 15 05:35 bianliang.sh
-rwxr-xr-x 1 root  root   112 Nov 14 17:12 case.sh
-rwxr-xr-x 1 root  root  2093 Nov 15 05:50 clean.sh

除了反引号之外,还可以使用()对变量进行赋值。

newer@ubuntu:~/script$ arch=$(uname -a);echo $arch
Linux ubuntu 4.2.0-27-generic #32~14.04.1-Ubuntu SMP Fri Jan 22 15:32:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

判断变量是否被初始化

if [ -z "$unassigned" ]
then
  echo "\$unassigned is NULL."
fi     # $unassigned is NULL.

相关文章

  • 目录

    shell 一、 初识shell二、 shell变量三、 正则表达式四、 认识脚本和shell判断表达式五、she...

  • shell(五)

    Shell 流程控制 和Java、PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): 在...

  • Shell(五)

    shell 的变量 对比区别: 除了反引号之外,还可以使用()对变量进行赋值。 判断变量是否被初始化

  • 五、Shell 注释

    欢迎加入技术交流群群号: 552340860 以"#"开头的行就是注释,会被解释器忽略。sh里没有多行注释,只能...

  • shell基础(五)

    一、循环终止的特殊命令 break、exit、continue、return的区别 例一: 效果如图: 实战一: ...

  • shell介绍、命令历史、命令补全和别名、通配符、输入输出重定向

    目录 一、shell介绍二、命令历史三、命令补全和别名四、通配符五、输入输出重定向 一、shell介绍 在计算机科...

  • shell脚本从入门到精通

    一、Shell脚本编写格式 二、回收站 三、当前内存使用率 四、倒计时程序的编写 五、水果商店 六、Shell基本...

  • shell技巧分享(五)

    这是一个系列文章,主要分享shell(部分功能仅适用于bash)的使用建议和技巧,每次分享3点,希望你能有所收获。...

  • 五、Shell echo命令

    Shell的echo 指令与PHP 的 echo 指令类似,都是用于字符串的输出。命令格式: 您可以使用echo实...

  • shell编程(五)变量

    变量分类 本地变量:用户私有变量,只有本用户可以使用,保存在家目录下的.bash_profile、.bashrc文...

网友评论

      本文标题:Shell(五)

      本文链接:https://www.haomeiwen.com/subject/vgvwuttx.html