LINUX环境变量(二)

一、Shell变量分为本地变量和环境变量。

1、本地变量:在用户现有运行的脚本中使用
 a) 定义本地变量 格式: variable-name=value
 b) 显示本地变量 格式: set 
 c) 清除本地变量 格式:unset variable-name 

2、环境变量:在所有的子进程中使用
 a) 定义环境变量 格式: export variable-name=value (与本地变量的定义相比,多了一个export关键字)
 b) 显示环境变量 格式: env (本地变量的显示使用set,环境变量的显示使用env)
 c) 清除环境变量 格式:unset variable-name (用法与本地变量相同,都使用unset)

二、用户登录后加载profile和bashrc的流程:

1) /etc/profile-------->/etc/profile.d/*.sh
2) $HOME/.bash_profile-------->$HOME/.bashrc---------->/etc/bashrc
说明:
1) bash首先执行/etc/profile脚本,/etc/profile脚本先依次执行/etc/profile.d/*.sh
2) 随后bash会执行用户主目录下的.bash_profile脚本,.bash_profile脚本会执行用户主目录下的.bashrc脚本, 而.bashrc脚本会执行/etc/bashrc脚本
至此,所有的环境变量和初始化设定都已经加载完成。

bash随后调用terminfo和inputrc,完成终端属性和键盘映射的设定.
其中PATH这个变量特殊说明一下:
如果是超级用户登录,在没有执行/etc/profile之前,PATH已经设定了下面的路径: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
如果是普通用户,PATH在/etc/profile执行之前设定了以下的路径: /usr/local/bin:/bin:/usr/bin
这里要注意的是:在用户切换并加载变量,例如su -,这时,如果用户自己切换自己,比如root用户再用su - root切换的话,加载的PATH和上面的不一样。准确的说,是不总是一样.所以,在/etc/profile脚本中,做了如下的配置:

if [ `id -u` = 0 ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
如果是超级用户登录,在/etc/profile.d/krb5.sh脚本中,在PATH变量搜索路径的最前面增加/usr/kerberos/sbin:/usr/kerberos/bin

如果是普通用户登录,在/etc/profile.d/krb5.sh脚本中,在PATH变量搜索路径的最前面增加/usr/kerberos/bin

在/etc/profile脚本中,会在PATH变量的最后增加/usr/X11R6/bin目录
在$HOME/.bash_profile中,会在PATH变量的最后增加$HOME/bin目录
以root用户为例,最终的PATH会是这样(没有其它自定义的基础上)
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
以alice用户(普通用户)为例
/usr/kerberos/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/alice/bin

*****************************************

~/.bash_profile 用户登录时被读取,其中包含的命令被执行

~/.bashrc 启动新的shell时被读取,并执行

~/.bash_logout shell 登录退出时被读取

参考资料:

一、Shell变量分为本地变量和环境变量。

1、本地变量:在用户现有运行的脚本中使用
 a) 定义本地变量 格式: variable-name=value
 b) 显示本地变量 格式: set 
 c) 清除本地变量 格式:unset variable-name 

2、环境变量:在所有的子进程中使用
 a) 定义环境变量 格式: export variable-name=value (与本地变量的定义相比,多了一个export关键字)
 b) 显示环境变量 格式: env (本地变量的显示使用set,环境变量的显示使用env)
 c) 清除环境变量 格式:unset variable-name (用法与本地变量相同,都使用unset)

二、用户登录后加载profile和bashrc的流程:

1) /etc/profile-------->/etc/profile.d/*.sh
2) $HOME/.bash_profile-------->$HOME/.bashrc---------->/etc/bashrc
说明:
1) bash首先执行/etc/profile脚本,/etc/profile脚本先依次执行/etc/profile.d/*.sh
2) 随后bash会执行用户主目录下的.bash_profile脚本,.bash_profile脚本会执行用户主目录下的.bashrc脚本, 而.bashrc脚本会执行/etc/bashrc脚本
至此,所有的环境变量和初始化设定都已经加载完成。

bash随后调用terminfo和inputrc,完成终端属性和键盘映射的设定.
其中PATH这个变量特殊说明一下:
如果是超级用户登录,在没有执行/etc/profile之前,PATH已经设定了下面的路径: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
如果是普通用户,PATH在/etc/profile执行之前设定了以下的路径: /usr/local/bin:/bin:/usr/bin
这里要注意的是:在用户切换并加载变量,例如su -,这时,如果用户自己切换自己,比如root用户再用su - root切换的话,加载的PATH和上面的不一样。准确的说,是不总是一样.所以,在/etc/profile脚本中,做了如下的配置:

if [ `id -u` = 0 ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
如果是超级用户登录,在/etc/profile.d/krb5.sh脚本中,在PATH变量搜索路径的最前面增加/usr/kerberos/sbin:/usr/kerberos/bin

如果是普通用户登录,在/etc/profile.d/krb5.sh脚本中,在PATH变量搜索路径的最前面增加/usr/kerberos/bin

在/etc/profile脚本中,会在PATH变量的最后增加/usr/X11R6/bin目录
在$HOME/.bash_profile中,会在PATH变量的最后增加$HOME/bin目录
以root用户为例,最终的PATH会是这样(没有其它自定义的基础上)
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
以alice用户(普通用户)为例
/usr/kerberos/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/alice/bin

*****************************************

~/.bash_profile 用户登录时被读取,其中包含的命令被执行

~/.bashrc 启动新的shell时被读取,并执行

~/.bash_logout shell 登录退出时被读取

参考资料:

猜你喜欢

转载自www.cnblogs.com/shujuxiong/p/9105321.html
今日推荐