Linux环境变量详解与应用

在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
....
原文地址:https://www.cnblogs.com/ghostwu/p/9074465.html