[root@localhost ~]# a=111 [root@localhost ~]# echo $a 111 [root@localhost ~]# set |grep 111 _=111 a=111 [root@localhost ~]#
[root@localhost ~]# [root@localhost ~]# a1=3 [root@localhost ~]# echo $a1 3 [root@localhost ~]# a_1=2 [root@localhost ~]# echo $a_1 2 [root@localhost ~]# _a1=4 [root@localhost ~]# echo $_a1 4 [root@localhost ~]# 1aa=2 变量名首位不能为数字 -bash: 1aa=2: 未找到命令 [root@localhost ~]#
[root@localhost ~]# a='a b c' [root@localhost ~]# echo $a a b c [root@localhost ~]# a="a b c" [root@localhost ~]# echo $a a b c
这里可使用 单引号'' 或 双引号"",但使用 单引号 更加好用——>方便脱义
[root@hf-01 ~]# a="a$bc" //会发现使用双引号,赋值变量得不到想要的结果 [root@hf-01 ~]# echo $a a [root@hf-01 ~]# a='a$bc' //使用单引号会发现正常赋值 [root@hf-01 ~]# echo $a a$bc
[root@hf-01 ~]# a=1 [root@hf-01 ~]# b=2 [root@hf-01 ~]# echo $a$b //变量$a=1,变量$b=2 12 [root@hf-01 ~]# a='a$bc' [root@hf-01 ~]# echo $a$b //变量$a=a$bc,变量$b=2 a$bc2 [root@hf-01 ~]# c="a$bc" [root@hf-01 ~]# echo $c //变量$bc为赋值,因此为空,最后输出的值为a a [root@hf-01 ~]# c="a$b"c [root@hf-01 ~]# echo $c //变量$b=2,,因此输出为a2c a2c
如下例子中,$bc为总体,而我又没有给它赋值,因此为空
当变量或表达式较为复杂的时候,变量叠加的时候,可使用双引号将它们标记起来mysql
[root@hf-01 ~]# w 05:34:28 up 4:05, 3 users, load average: 0.00, 0.01, 0.05 USER TTY LOGIN@ IDLE JCPU PCPU WHAT root tty1 01:29 4:04m 0.03s 0.03s -bash root pts/0 01:30 4.00s 0.07s 0.03s w root pts/1 05:34 5.00s 0.02s 0.02s -bash
[root@hf-01 ~]# echo $SSH_TTY //当前是在/dev/pts/0下 /dev/pts/0
- 在终端2下执行命令 echo $SSH_TTY
[root@hf-01 ~]# echo $SSH_TTY /dev/pts/1
[root@hf-01 ~]# hanfeng=linux [root@hf-01 ~]# echo $hanfeng linux
[root@hf-01 ~]# echo $hanfeng //会发现变量为 空 [root@hf-01 ~]#
[root@hf-01 ~]# bash [root@hf-01 ~]#
[root@hf-01 ~]# pstree systemd─┬─NetworkManager───3*[{NetworkManager}] ├─auditd───{auditd} ├─avahi-daemon───avahi-daemon ├─crond ├─dbus-daemon───{dbus-daemon} ├─firewalld───{firewalld} ├─iprdump ├─iprinit ├─iprupdate ├─login───bash ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─mysqld_safe───mysqld───20*[{mysqld}] ├─polkitd───5*[{polkitd}] ├─rsyslogd───2*[{rsyslogd}] ├─sshd───sshd─┬─bash───bash───pstree │ └─bash ├─systemd-journal ├─systemd-logind ├─systemd-udevd └─tuned───4*[{tuned}] [root@hf-01 ~]#
[root@hf-01 ~]# echo $hanfeng [root@hf-01 ~]#
这是由于这个变量仅仅在上一个shell中
[root@hf-01 ~]# exit exit [root@hf-01 ~]# pstree systemd─┬─NetworkManager───3*[{NetworkManager}] ├─auditd───{auditd} ├─avahi-daemon───avahi-daemon ├─crond ├─dbus-daemon───{dbus-daemon} ├─firewalld───{firewalld} ├─iprdump ├─iprinit ├─iprupdate ├─login───bash ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─mysqld_safe───mysqld───20*[{mysqld}] ├─polkitd───5*[{polkitd}] ├─rsyslogd───2*[{rsyslogd}] ├─sshd───sshd─┬─bash───pstree │ └─bash ├─systemd-journal ├─systemd-logind ├─systemd-udevd └─tuned───4*[{tuned}] [root@hf-01 ~]#
这时会看到缺乏了一个bash,由于刚exit退出了一个bash
[root@hf-01 ~]# echo $hanfeng linux
这种定义一个变量叫作非全局,或者叫作本地的变量(仅仅在你终端下生效)linux
[root@hf-01 ~]# export hanfeng=linux [root@hf-01 ~]# echo $hanfeng linux [root@hf-01 ~]# bash [root@hf-01 ~]# echo $hanfeng linux [root@hf-01 ~]#
全局环境变量,在终端1下,在打开shell以后,只要执行export 命令 ,在这下面全部的子shell 都会变量值,但在终端2下,变量依旧是不会生效 全局变量是向下的,在这个shell的基础上生成子shell,子子shell,子子子shell,而不会向上生效
[root@hf-01 ~]# hanfeng=linux [root@hf-01 ~]# echo $hanfeng linux [root@hf-01 ~]# unset hanfeng [root@hf-01 ~]# echo $hanfeng [root@hf-01 ~]#