[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 ~]#