/etc/profile是全局的,是私有的linux
/etc/profile用于整个系统全部用户, ~/.bash_profile, ~/.profile和~/.bashrc 用于各个用户,这里的"~"符号就是各当前用户的$HOME ~/.bash_profile 和 ~/.profile 只在登录时读取一次。 ~/.bashrc 每次都读取 ~/.bash_profile 和 ~/.profile 的区别在于,bash 只读取~/.bash_profile;而对于 ~/.profile 来讲,不光bash, 其余shell 也读 ~/.profile
(1)/etc/profile(全局变量): 为系统的每一个用户设置环境信息,当用户第一次登陆时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。shell
(2)/etc/bashrc: 为每个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。bash
(3)~/.bash_profile: 每一个用户均可使用该文件输入专用于本身使用的shell信息,当用户登陆时,该文件仅仅执行一次!默认状况下,他设置一些环境变量,执行用户的.bashrc文件。.net
(4)~/.bashrc: 该文件包含专用于你的bash shell的bash信息,当登陆时以及每次打开新的shell时,该该文件被读取。code
(5)~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件. 另外,/etc/profile中设定的变量(全局)的能够做用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc /profile中的变量,他们是”父子”关系。blog
(6)~/.bash_profile 是交互式、login 方式进入 bash 运行的~/.bashrc 是交互式 non-login 方式进入 bash 运行的一般两者设置大体相同,因此一般前者会调用后者。继承
在登陆Linux时要执行文件的过程以下:作用域
在刚登陆Linux时,首先启动 /etc/profile 文件,而后再启动用户目录下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一个,执行的顺序为:~/.bash_profile、 ~/.bash_login、 ~/.profile。若是 ~/.bash_profile文件存在的话,通常还会执行 ~/.bashrc文件。由于在 ~/.bash_profile文件中通常会有下面的代码:get
if [ -f ~/.bashrc ] ; thenclass
. ./bashrc
fi
~/.bashrc中,通常还会有如下代码:
if [ -f /etc/bashrc ] ; then
. /bashrc
fi
因此,~/.bashrc会调用 /etc/bashrc文件。最后,在退出shell时,还会执行 ~/.bash_logout文件。
执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout
参考:
http://dblab.xmu.edu.cn/blog/linux-environment-variable/
https://blog.csdn.net/caiwenfeng_for_23/article/details/44242961