|NO.Z.00044|——————————|LinuxShell|——|Linux&Shell$变量.V01|

一、Bash的变量和运算符
### --- 什么是变量

~~~     # 在定义变量时,有一些规则需要遵守:
~~~     变量名称可以由字母、数字和下划线组成,但是不能以数字开头。
~~~     如果变量名是“2name”则是错误的。
~~~     在 Bash 中,变量的默认类型都是字符串型,
~~~     如果要进行数值运算,则必修指定变量类型为数值型。

~~~     变量用等号连接值,等号左右两侧不能有空格。
~~~     变量的值如果有空格,需要使用单引号或双引号包括。
~~~     如:“test="hello world!"”。
~~~     其中双引号括起来的内容“$”、“\”和反引号都拥有特殊含义,
~~~     而单引号括起来的内容都是普通字符。在变量的值中,
~~~     可以使用“\”转义符。如果需要增加变量的值,那么可以进行变量值的叠加。
~~~     不过变量需要用双引号包含"$变量名"或用${变量名}包含变量名。例如:
### --- 叠加变量 test,变量值变成了 123456

[root@localhost ~]# test=123
[root@localhost ~]# test="$test"456
[root@localhost ~]# echo $test
123456
### --- 再叠加变量 test,变量值编程了 123456789

[root@localhost ~]# test=${test}789
[root@localhost ~]# echo $test
123456789
### --- 变量值的叠加可以使用两种格式:
~~~     “$变量名”或${变量名}如果是把命令的结果作为变量值赋予变量,
~~~     则需要使用反引号或$()包含命令。例如:
~~~     2018 年 10 月 21 日 星期一 20:27:50 CST
~~~     环境变量名建议大写,便于区分。

[root@localhost ~]# test=$(date)
[root@localhost ~]# echo $test

 

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-03-26 23:20  yanqi_vip  阅读(16)  评论(0)    收藏  举报

导航