execute /etc/profile
IF ~/.bash_profile exists THEN
execute ~/.bash_profile
ELSE
IF ~/.bash_login exist THEN
execute ~/.bash_login
ELSE
IF ~/.profile exist THEN
execute ~/.profile
END IF
END IF
END IF
2. Non-Login Shell 初始化时配置文件读取顺序的伪代码示意:
execute /etc/bash.bashrc
IF ~/.bashrc exists THEN
execute ~/.bashrc
END IF
几个bash配置文件的说明:
另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系.
其实这个问题的核心就是 Shell 初始化时读取配置文件的步骤,而 Shell 又可以分为两类:Login Shell 和 Non-login Shell。参考博客 Execution sequence for .bash_profile,...:
1. Login Shell 初始化时配置文件读取顺序的伪代码示意:
execute /etc/profile IF ~/.bash_profile exists THEN execute ~/.bash_profile ELSE IF ~/.bash_login exist THEN execute ~/.bash_login ELSE IF ~/.profile exist THEN execute ~/.profile END IF END IF END IF2. Non-Login Shell 初始化时配置文件读取顺序的伪代码示意:
execute /etc/bash.bashrc IF ~/.bashrc exists THEN execute ~/.bashrc END IF最后,Mac 的终端默认开启为 Login Shell。而 Ubuntu 的 Gnome Terminal 默认开启的是 Non-Login Shell.
~/.bash_profile只在当前用户登入的时候加载,~/.bashrc在每次 Bash 初始化的时候都会加载。经常看到 .bash_profile 里的内容是
if [ -e ~/.bashrc ]; then source ~/.bashrc fi有时候我也搞不清其中的关系,不过我一般都是把启动配置放在.bashrc下面,如果不管用的话,再创建上述.bash_profile。