Linux環境變數詳解與應用

ghostwu發表於2018-05-22

在bash shell中,環境變數分為:

>全域性變數

>區域性變數

全域性變數,不僅對shell可見,對其子程式也可見

檢視預設的全域性環境變數:

ghostwu@dev:~$ printenv
ghostwu@dev:~$ env

這兩個命令都可以列印全域性環境變數

ghostwu@dev:~$ printenv | grep HOME
HOME=/home/ghostwu
ghostwu@dev:~$ 

HOME是一個全域性環境變數,儲存使用者的家目錄

ghostwu@dev:~$ echo $HOME
/home/ghostwu

上面說了,全域性環境變數對子shell也有用,我們就開啟一個子程式,來驗證一下:

ghostwu@dev:~$ bash
ghostwu@dev:~$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
ghostwu   4647  4642  1 22:25 pts/11   00:00:00 bash
ghostwu   4659  4647  3 22:25 pts/11   00:00:00 bash
ghostwu   4670  4659  0 22:25 pts/11   00:00:00 ps -f
ghostwu@dev:~$ echo $HOME
/home/ghostwu
ghostwu@dev:~$ exit
exit
ghostwu@dev:~$ echo $HOME
/home/ghostwu

由此可見,在當前的shell 以及子shell中 都能訪問到全域性環境變數

 

區域性環境變數:只能在定義他們他們的程式中可見

設定區域性變數,跟php的語法差不多,只不過不需要美元符號,讀取區域性變數需要用$符號

ghostwu@dev:~$ echo $my_var

讀取一個沒有定義的區域性環境變數,值為空

ghostwu@dev:~$ echo $my_var

ghostwu@dev:~$ my_var=ghostwu
ghostwu@dev:~$ echo $my_var
ghostwu

定義區域性變數的=號左右不要用空格,否則會被當做命令執行

ghostwu@dev:~$ myvar = `hello`
myvar: command not found

在子shell中,是不能訪問到父shell(程式)定義的區域性變數.

ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
ghostwu   4647  4642  0 22:25 pts/11   00:00:00 bash
ghostwu   5372  4647  0 22:31 pts/11   00:00:00 ps -f
ghostwu@dev:~$ bash
ghostwu@dev:~$ echo $my_var

ghostwu@dev:~$ exit
exit
ghostwu@dev:~$ echo $my_var
ghostwu

如果需要在子shell中訪問到父shell中定義的區域性變數,我們可以把區域性變數匯入到全域性變數中,怎麼匯入?用export 變數名稱 即可,匯入的時候,變數名稱前面

不要用$符號

ghostwu@dev:~$ printenv | grep my_var
ghostwu@dev:~$ export my_var
ghostwu@dev:~$ !p
printenv | grep my_var
my_var=ghostwu
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ bash
ghostwu@dev:~$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
ghostwu   5790  5785  0 22:33 pts/11   00:00:00 bash
ghostwu   5835  5790  6 22:34 pts/11   00:00:00 bash
ghostwu   5846  5835  0 22:34 pts/11   00:00:00 ps -f
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ exit
exit
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ printenv | grep my_var
my_var=ghostwu

刪除環境變數,用unset。

>在父shell中,刪除一個匯入到全域性變數的 環境變數,之後就訪問不到這個變數了

ghostwu@dev:~$ printenv | grep my_var
my_var=ghostwu
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ unset my_var
ghostwu@dev:~$ echo $my_var

ghostwu@dev:~$ bash
ghostwu@dev:~$ echo $my_var

ghostwu@dev:~$ exit
exit

 

>如果在子shell中,匯入一個全域性變數,然後刪除,那麼子shell中訪問不到,但是在父shell中,還可以訪問到

ghostwu@dev:~$ echo $my_var

ghostwu@dev:~$ my_var="ghostwu"
ghostwu@dev:~$ export my_var
ghostwu@dev:~$ printenv | grep my_var
my_var=ghostwu
ghostwu@dev:~$ bash
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ unset my_var
ghostwu@dev:~$ echo $my_var

ghostwu@dev:~$ exit
exit
ghostwu@dev:~$ echo $my_var
ghostwu

 

PATH環境變數,定義了一堆路徑,這些路徑的作用是,當命令列輸入一個命令的時候,在去PATH變數中定義的路徑中去尋找。所以,如果想讓一個自定義的可執行

程式能夠在任意目錄下執行,就需要把這個可執行的程式所在的目錄設定到PATH環境變數。怎麼設定呢? 在PATH變數中,路徑用:號分隔。

假如,我在root的家目錄下面建立了一個bin目錄,用來放一個shell指令碼,名字為ghost

ghostwu@dev:~/bin$ pwd
/home/ghostwu/bin
ghostwu@dev:~/bin$ ls -l
total 4
-rwxrwxr-x 1 ghostwu ghostwu 20 5月  22 22:44 ghost
ghostwu@dev:~/bin$ cat ghost 
#!/bin/bash
ls -l /
ghostwu@dev:~/bin$ ./ghost 
total 105
drwxr-xr-x   2 root root  4096 5月  17 23:16 bin
drwxr-xr-x   4 root root  1024 2月  10 16:23 boot
drwxr-xr-x   2 root root  4096 2月  10 16:15 cdrom
drwxr-xr-x  20 root root  4400 5月  22 22:16 dev
drwxr-xr-x 139 root root 12288 5月  18 05:11 etc
drwxr-xr-x   4 root root  4096 2月  10 16:16 home
lrwxrwxrwx   1 root root    33 2月  10 16:17 initrd.img -> boot/initrd.img-4.10.0-28-generic
drwxr-xr-x  22 root root  4096 5月  17 23:52 lib
drwxr-xr-x   2 root root  4096 5月  17 23:39 lib64
drwx------   2 root root 16384 2月  10 16:12 lost+found
drwxr-xr-x   3 root root  4096 2月   9 16:34 media
drwxr-xr-x   3 root root  4096 4月   7 11:10 mnt
drwxr-xr-x   4 root root  4096 5月  17 23:22 opt
drwxr-xr-x   2 root root  4096 5月  18 05:12 patch
dr-xr-xr-x 245 root root     0 5月  22 22:16 proc
drwx------   9 root root  4096 5月  20 06:57 root
drwxr-xr-x  29 root root   900 5月  22 22:21 run
drwxr-xr-x   2 root root 12288 2月  10 16:24 sbin
drwxr-xr-x   2 root root  4096 4月  29  2017 snap
drwxr-xr-x   2 root root  4096 8月   1  2017 srv
dr-xr-xr-x  13 root root     0 5月  22 22:16 sys
drwxrwxrwt  14 root root  4096 5月  22 22:44 tmp
drwxr-xr-x  11 root root  4096 8月   1  2017 usr
drwxr-xr-x  15 root root  4096 5月  17 23:28 var
lrwxrwxrwx   1 root root    30 2月  10 16:17 vmlinuz -> boot/vmlinuz-4.10.0-28-generic
drwxr-xr-x   6 root root  4096 5月  17 23:19 www

由於我的PATH變數,已經包含了/home/ghostwu/bin這個路徑,所以在任意的目錄下,都會搜素這個目錄,就會執行ghost

ghostwu@dev:~$ cd /etc
ghostwu@dev:/etc$ ghost
total 105
drwxr-xr-x   2 root root  4096 5月  17 23:16 bin
drwxr-xr-x   4 root root  1024 2月  10 16:23 boot
drwxr-xr-x   2 root root  4096 2月  10 16:15 cdrom
drwxr-xr-x  20 root root  4400 5月  22 22:16 dev
drwxr-xr-x 139 root root 12288 5月  18 05:11 etc
drwxr-xr-x   4 root root  4096 2月  10 16:16 home
lrwxrwxrwx   1 root root    33 2月  10 16:17 initrd.img -> boot/initrd.img-4.10.0-28-generic
drwxr-xr-x  22 root root  4096 5月  17 23:52 lib
drwxr-xr-x   2 root root  4096 5月  17 23:39 lib64
drwx------   2 root root 16384 2月  10 16:12 lost+found
drwxr-xr-x   3 root root  4096 2月   9 16:34 media
drwxr-xr-x   3 root root  4096 4月   7 11:10 mnt
drwxr-xr-x   4 root root  4096 5月  17 23:22 opt
drwxr-xr-x   2 root root  4096 5月  18 05:12 patch
dr-xr-xr-x 245 root root     0 5月  22 22:16 proc
drwx------   9 root root  4096 5月  20 06:57 root
drwxr-xr-x  29 root root   900 5月  22 22:21 run
drwxr-xr-x   2 root root 12288 2月  10 16:24 sbin
drwxr-xr-x   2 root root  4096 4月  29  2017 snap
drwxr-xr-x   2 root root  4096 8月   1  2017 srv
dr-xr-xr-x  13 root root     0 5月  22 22:45 sys
drwxrwxrwt  14 root root  4096 5月  22 22:44 tmp
drwxr-xr-x  11 root root  4096 8月   1  2017 usr
drwxr-xr-x  15 root root  4096 5月  17 23:28 var
lrwxrwxrwx   1 root root    30 2月  10 16:17 vmlinuz -> boot/vmlinuz-4.10.0-28-generic
drwxr-xr-x   6 root root  4096 5月  17 23:19 www

在ubuntu16.04下面,這個路徑設定是在.profile這個檔案中的,而使用者登入系統時,會執行這個.profile,所以PATH中的設定就會生效,接下來把他刪除.

ghostwu@dev:~$ tail -1 .profile 
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

刪除之後,你會發現,他依然存在,但是檔案中確實是刪除了,這個時候,我們要重啟系統.

ghostwu@dev:~$ vim .profile 
ghostwu@dev:~$ tail -2 .profile 
#PATH="$HOME/bin:$HOME/.local/bin:$PATH"
PATH="$HOME/.local/bin:$PATH"
ghostwu@dev:~$ echo $PATH
/home/ghostwu/bin:/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

 登出系統也可以,我們刪除的路徑,已經生效了

ghostwu@dev:~$ echo $PATH
/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ghostwu@dev:~$ cd /etc
ghostwu@dev:/etc$ ghost

ghostwu@dev:/etc$ 

我們可以臨時把路徑設定回去,在bin目錄下建立一個指令碼ghost2.,沒有設定路徑之前,ghost2是找不到的

ghostwu@dev:~$ echo $PATH
/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ghostwu@dev:~$ cat ~/bin/ghost2
#!/bin/bash
ls -l /etc
ghostwu@dev:~$ ghost2
ghost2: command not found

現在,就生效了,但是這個生效,在當前shell程式退出,或者另外的shell裡面訪問,是訪問不到的,如果我們想永久讓設定的環境儲存下來,應該把他寫在檔案中,

一般寫在下面4個檔案中

/etc/profile: 這個是所有登入的使用者都會載入的檔案

$HOME/.bash_profile

$HOME/.bash_login

$HOME/.profile

後面3個檔案是使用者專用。我們之前的變數就是設定在.profile中。其實,還有一個檔案,也可以設定:.bashrc。為甚呢?因為.profile會判斷是否存在.bashrc。從而

載入.bashrc。所以在.bashrc中設定的環境變數,也會永久儲存在當前使用者環境下.

ghostwu@dev:~$ echo $PATH
/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ghostwu@dev:~$ cat ~/bin/ghost2
#!/bin/bash
ls -l /etc
ghostwu@dev:~$ ghost2
ghost2: command not found
ghostwu@dev:~$ 
ghostwu@dev:~$ PATH=$PATH:$HOME/bin
ghostwu@dev:~$ echo $PATH
/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ghostwu/bin
ghostwu@dev:~$ ghost2
total 1252
drwxr-xr-x  3 root  root     4096 8月   1  2017 acpi
....

 

相關文章