shell中的变量讲解

1.用户变量

1.变量名和变量值之间赋值符号(=)☆☆左右两侧绝对不能有空格☆☆,否则报错。重点
2.和python一样,shell脚本语言是一种弱类型语言,在脚本当中使用变量不需要也无法指定变量的“类型”。
3.默认状态下,shell脚本的变量都是字符串,即一连串的单词列表。
4.变量的命名,和其他语言一样,只能包含字母、数字、下划线,且必须以字母、下划线开头,不能以数字开头。
5.shell中的解释器,在通过$引用变量时,若此变量没有定义,则不会报错,会输出为空。
a=10
b=111
al=abc

2.变量的引用

定义的变量如何使用
$变量名 和${变量名} 两种方式都可以。
1.$变量名,通过这种方式引用变量时,变量名后面 不能紧跟其他内容,但可以用空格和其后的内容分割开来。
2.${变量名},通过这种方式引用变量时,变量名后面 可以紧跟其他内容。因为bash解释器会自动识别${变量名}。
代码演示:

a=1
echo this is num $a
this is num 1
echo this is num $a good
this is num 1 good

echo this is num $agood #解释器会把agood当前变量名,因为没有定义变量agood,所以输出为空
this is num

echo this is num ${
    
    a}
this is num 1

echo this is num ${
    
    a}good
this is num 1good

echo this is num ${
    
    a} good
this is num 1 good

其他待补充

猜你喜欢

转载自blog.csdn.net/adminstate/article/details/131061579