Explanation of variables in the shell

1. User variables

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

2. Variable reference


$How to use the variable name and ${variable name} of the defined variable can be used in both ways.
1. $Variable name. When referencing a variable in this way, the variable name cannot be followed by other content, but it can be separated by a space and the subsequent content.
2. ${variable name}, when referencing a variable in this way, the variable name can be followed by other content. Because the bash interpreter will automatically recognize ${variable name}.
Code demo:

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

Other to be added

Guess you like

Origin blog.csdn.net/adminstate/article/details/131061579