Linux|Unix中Bash环境变量的设定顺序

来源:互联网 发布:加油站销售软件 编辑:程序博客网 时间:2024/05/21 22:41
和Bash的环境设定有关的文件有
/etc/profile (主要)
$HOME/.bash_profile (主要)
$HOME/.bash_login
$HOME/.profile
$HOME/.bash_logout (主要)
$HOME/.bashrc (主要)
/etc/bashrc

说明如下:
* 登入(login)时
# 先执行 /etc/profile
# 接着bash会检查使用者的自家目录中,是否有 .bash_profile 或者 .bash_login或者 .profile,若有,则会执行其中一个,执行顺序为:
# .bash_profile 最优先
# .bash_login其次
# .profile 最后
这三个档案只有在登入时,才会被bash读取

* 登出(exit/logout)时
bash会检查使用者自家目录中是否有 .bash_logout,若有,则bash会执行其中的指令

* 登入后启动一个新的 shell :
此时我们称之为一个 subshell, 也就是说在命令列中键入 bash, 除了原先登入时的bash之外, 又另外启动了一个新的 bash shell.
bash 会检查使用者的自家目录中是否有 .bashrc,若有则予以执行

* 测试:
在各档中加入 echo 指令, 以观察其执行顺序.

各文件用途说明
* /etc/profile 由 root 所控制, 用来设定适合全体使用者的shell环境
* 若使用者自己觉得 /etc/profile 的设定, 并不合意, 可以修改自家目录中的 .bash_profile
* 既然有了 .bash_profile, 为何要有 .bash_login 及 .profile ?这是因为有些人可能是从Bourne shell 移转过来的, 那么, 只要将 Bourne shell 主要的起动档 .profile 移到自家目录中, 放弃使用 .bash_profile 及 .bash_login 即可继续沿用以前的设定环境
* .bash_login 存在理由或许和 c shell 有关, 但因为 bash 和 c shell 二者语法并不完全相容, 因此, 并不建议将 c shell 的启动档直接移过来使用.
* .bashrc 则是用来设定 subshell 的环境的, 之所以要有这个 .bashrc 是为免 subshell产生时, 又重覆将 /etc/profile 执行一次. 我们发现 .bashrc 中已预先会去执行 /etc/bashrc的指令, 这表示, 或许 root 会将产生 subshell 时的环境设好了,使用者只要沿用 /etc/bashrc的内容, 应该不会有任何问题.
* .bash_logout 是使用者登出主机之前, 会去执行的设定档, 如果使用者希望在他登出系统之后, 能帮他自动处理一些琐事, 比如: 清除临时文件, 清除屏幕等, 可以在这个档案中加以设定.<script type="text/javascript"><!--google_ad_client = "ca-pub-7104628658411459";/* wide2 */google_ad_slot = "5922063526";google_ad_width = 468;google_ad_height = 60;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击