关于“.bash_profile”和“.bashrc”区别的总结
bash的startup文件
Linux shell是用户与Linux系统进行交互的媒介,而bash作为目前Linux系统中最常用的shell,它支持的startup文件也并不单一,甚至容易让人感到费解。本文以CentOS7系统为例,对bash的startup文件进行一些必要的梳理和总结。
根据bash手册上的描述:
- /etc/profile
The systemwide initialization file, executed for login shells- /etc/bash.bash_logout
The systemwide login shell cleanup file, executed when a login shell exits- ~/.bash_profile
The personal initialization file, executed for login shells- ~/.bashrc
The individual per-interactive-shell startup file- ~/.bash_logout
The individual login shell cleanup file, executed when a login shell exits
此外,bash还支持~/.bash_login
和~/.profile
文件,作为对其他shell的兼容,它们与~/.bash_profile
文件的作用是相同的。
备注:Debian系统会使用~/.profile
文件取代~/.bash_profile
文件,因此在相关细节上,会与CentOS略有不同。
“profile”与“rc”系列
通过名字的不同,我们可以直观地将startup文件分为“profile”与“rc”两个系列,其实他们的功能都很类似,但是使用的场景不同,这也是大家最容易忽略的地方。
所谓的不同场景,其实就是shell的运行模式。我们知道运行中的bash有“交互”和“登陆”两种属性,而执行“profile”系列还是“rc”系列,就与shell的这两个属性有关。
关于bash的运行模式,请参见我的另一篇博客:
《关于“交互式-非交互式”与“登录-非登陆”shell的总结》
原理上讲,“登陆shell”启动时会加载“profile”系列的startup文件,而“交互式非登陆shell”启动时会加载“rc”系列的startup文件。
“profile”系列的执行场景
根据bash手册上的描述:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the
--login
option, it first reads and executes commands from the file/etc/profile
, if that file exists. After reading that file, it looks for~/.bash_profile
,~/.bash_login
, and~/.profile
, in that order, and reads and executes commands from the first one that exists and is readable. The--noprofile
option may be used when the shell is started to inhibit this behavior.
When a login shell exits, bash reads and executes commands from the files~/.bash_logout
and/etc/bash.bash_logout
, if the files exists.
“profile”系列的代表文件为~/.bash_profile
,它用于“登录shell”的环境加载,这个“登录shell”既可以是“交互式”的,也可以是“非交互式”的。
通过--noprofile
选项可以阻止系统加载“profile”系列的startup文件。
交互式登陆shell
对于交互式的登陆shell而言,CentOS规定了startup文件的加载顺序如下:
登陆过程:
1. 读取并执行/etc/profile
文件;
2. 读取并执行~/.bash_profile
文件;
- 若文件不存在,则读取并执行~/.bash_login
文件;
- 若文件不存在,则读取并执行~/.profile
文件;
登出过程:
1. 读取并执行~/.bash_logout
文件;
2. 读取并执行/etc/bash.bash_logout
文件;
为了完成实验,我新建了一些系统默认没有提供的startup文件,例如/etc/bash.bash_logout
。然后在每个文件中打印了文件名,并将它们之间的显式调用语句注释掉,例如~/.bash_profile
对~/.bashrc
的显式调用。
“交互式登陆shell”的实验结果如下:
[root@localhost ~]# su - chen
Last login: Tue Apr 18 17:15:08 CST 2017 from 192.168.161.1 on pts/2
execute /etc/profile
execute ~/.bash_profile
-bash-4.2$ exit
logout
execute ~/.bash_logout
execute /etc/bash.bash_logout
[root@localhost ~]#
我们看到,因为执行了~/.bash_profile
文件,所以优先级更低的~/.bash_login
和~/.profile
文件并没有被执行。
我们可以删除~/.bash_profile
和~/.bash_login
文件,这样系统就会找到并执行~/.profile
文件:
[root@localhost ~]# mv /home/chen/.bash_profile /home/chen/.bash_profile.bak
[root@localhost ~]# mv /home/chen/.bash_login /home/chen/.bash_login.bak
[root@localhost ~]# su - chen
Last login: Tue Apr 18 17:27:21 CST 2017 on pts/1
execute /etc/profile
execute ~/.profile
-bash-4.2$ exit
logout
execute ~/.bash_logout
execute /etc/bash.bash_logout
[root@localhost ~]#
非交互式登陆shell
对于非交互式的登陆shell而言,CentOS规定了startup文件的加载顺序如下:
登陆过程:
1. 读取并执行/etc/profile
文件;
2. 读取并执行~/.bash_profile
文件;
- 若文件不存在,则读取并执行~/.bash_login
文件;
- 若文件不存在,则读取并执行~/.profile
文件;
我们注意到,与“交互式登陆shell”相比,“非交互式登陆shell”并没有登出的过程,实验也证实了这一点:
-bash-4.2$ bash --login -c "uname -r"
execute /etc/profile
execute ~/.bash_profile
3.10.0-514.el7.x86_64
-bash-4.2$ # 此时非交互式shell已退出
“rc”系列的执行场景
根据bash手册上的描述:
When an interactive shell that is not a login shell is started, bash reads and executes commands from
~/.bashrc
, if that file exists. This may be inhibited by using the--norc
option. The--rcfile
file option will force bash to read and execute commands from file instead of~/.bashrc
.
“rc”系列的代表文件为~/.bashrc
,它用于“交互式非登录shell”的环境加载。
通过--norc
选项可以阻止系统加载“rc”系列的startup文件;通过--rcfile
选项可以使用指定的文件替代系统默认的~/.bashrc
文件。
交互式非登陆shell
对于交互式的非登陆shell而言,CentOS规定了startup文件的加载顺序如下:
1. 读取并执行~/.bashrc
或--rcfile
选项指定的文件
这里需要说明,其实“rc”系列startup文件还包括/etc/bashrc
。但是系统并不直接调用这个文件,而是通过~/.bashrc
文件显式地调用它。
为了完成实验,我在每个startup文件中打印了文件名,并将它们之间的显式调用语句注释掉,例如~/.bashrc
对/etc/bashrc
的显式调用。
“交互式非登陆shell”的实验结果如下:
[root@localhost ~]# su chen
execute ~/.bashrc
bash-4.2$ exit
exit
[root@localhost ~]#
startup文件的默认调用关系
细心的用户会发现,startup文件的加载并不像上面所述的那样简单。这是因为在CentOS中,startup文件之间还存在着默认的显式调用关系,它们是:
1. ~/.bash_profile
显式调用~/.bashrc
文件;
2. ~/.bashrc
显式调用/etc/bashrc
文件;
再看startup文件
分别打开/etc/profile
和/etc/bashrc
两个文件,我们可以看到:
[root@localhost ~]# head /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
[root@localhost ~]# head /etc/bashrc
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
由此可见,“profile”系列文件的主要目的在于为“登录shell”设置环境变量和启动程序;而“rc”系列文件的主要目的在于设置功能和别名。
顺便提一句,Linux中“rc”是英文“run command”的缩写,表示文件中存放需要执行的命令。其实这也非常符合逻辑,设置功能就要执行shopt
命令,而设置别名要执行alias
命令。与“rc”系列互补,“profile”系列用来设置环境变量,它不会去调用这两个命令,但却经常需要使用export
语句。不信你可以看一看这两个文件。
另外值得一提的是,这两个文件同时提到了一个位置:/etc/profile.d
目录。这个目录用于存放个性化配置脚本,你可以把自己需要的全局配置放入以.sh
结尾的文件中,系统在执行/etc/profile
和/etc/bashrc
文件时,都会择机调用它们。这样做最大的好处是便于维护,而且相对更加安全。
这些文件的编写方法,可以参考目录下已有的文件:
[root@localhost ~]# ls /etc/profile.d/*.sh
/etc/profile.d/256term.sh /etc/profile.d/colorls.sh /etc/profile.d/less.sh
/etc/profile.d/colorgrep.sh /etc/profile.d/lang.sh /etc/profile.d/which2.sh
总结
对于“登录shell”而言,“交互式”执行“登陆”和“登出”相关的“profile”系列startup文件,“非交互式”只执行“登陆”相关的“profile”系列startup文件;对于“非登陆shell”而言,“交互式”执行“rc”系列的startup文件,而“非交互式”执行的配置文件由环境变量BASH_ENV
指定。
Linux中startup文件区分全局和个人:全局startup文件放在/etc
目录下,用于设置所有用户共同的配置,除非你清楚地知道你在做的事情,否则不要轻易改动它们;个人startup文件放在~
目录下,用于设置某个用户的个性化配置。
~/.bash_profile
会显式调用~/.bashrc
文件,而~/.bashrc
又会显式调用/etc/bashrc
文件,这是为了让所有交互式界面看起来一样。无论你是从远程登录(登陆shell),还是从图形界面打开终端(非登陆shell),你都拥有相同的提示符,因为环境变量PS1
在/etc/bashrc
文件中被统一设置过。
下面我来对startup文件进行一个完整的总结:
startup文件 | 交互登陆 | 非交互登陆 | 交互非登陆 | 非交互非登陆 |
---|---|---|---|---|
/etc/profile | 直接执行1 | 直接执行1 | - | - |
~/.bash_profile | 直接执行2 | 直接执行2 | - | - |
~/.bash_login | 条件执行2 | 条件执行2 | - | - |
~/.profile | 条件执行2 | 条件执行2 | - | - |
~/.bash_logout | 直接执行3 | 不执行 | - | - |
/etc/bash.bash_logout | 直接执行4 | 不执行 | - | - |
~/.bashrc | 引用执行2.1 | 引用执行2.1 | 直接执行1 | - |
/etc/bashrc | 引用执行2.2 | 引用执行2.2 | 引用执行1.1 | - |
备注:
1. “直接执行”表示此文件被系统直接调用,它的执行是无条件的;
2. “条件执行”表示此文件被系统调用是有先决条件的(没有优先级更高的文件可用);
3. “引用执行”表示此文件不是被系统直接调用的,而是被其他文件显式调用的;
4. 后面的数字表示文件被调用的顺序,数字越大调用越靠后;
5. “非交互非登陆”shell的配置文件可以由BASH_ENV
环境变量指定;
最后我想说的是,知道startup文件何时被执行并不是关键,关键是要理解自己的情况应该去修改哪个startup文件。
如果你想对bash的功能进行设置或者是定义一些别名,推荐你修改~/.bashrc
文件,这样无论你以何种方式打开shell,你的配置都会生效。而如果你要更改一些环境变量,推荐你修改~/.bash_profile
文件,因为考虑到shell的继承特性,这些更改确实只应该被执行一次(而不是多次)。针对所有用户进行全局设置,推荐你在/etc/profile.d
目录下添加以.sh
结尾的文件,而不是去修改全局startup文件。
关于“.bash_profile”和“.bashrc”区别的总结的更多相关文章
- linux bash_profile和.bashrc区别
经常在一些技术类的文章中提到修改bash_profile和.bashrc这两个文件,也算是使用频率比较高的两个文件吧,但实现同样一个功能,有的教程里说修改bash_profile这个文件,有的教程里却 ...
- bash_profile和bashrc区别
[.bash_profile 与 .bashrc 的区别].bash_profile is executed for login shells, while .bashrc is executed f ...
- .bash_profile和.bashrc的区别(如何设置生效)
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...
- profile bashrc bash_profile之间的区别和联系
profile bashrc bash_profile之间的区别和联系 博客分类: Linux 执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bas ...
- Linux下环境变量配置方法梳理(.bash_profile和.bashrc的区别)
在linux系统下,如果下载并安装了应用程序,在启动时很有可能在键入它的名称时出现"command not found"的提示内容.如果每次都到安装目标文件夹内,找到可执行文件来进 ...
- 2018/05/02 每日一学Linux 之 .bash_profile和.bashrc的区别
最近一直在学习其他,导致博客就疏忽了,很不好(其实就是自己懒了......). -- 为什么要使用 .bash_profile和.bashrc ? 在平常的使用中,有些文件夹或者命令很长,在执行时需要 ...
- Linux下环境变量(.bash_profile和.bashrc的区别)
在linux系统下,如果下载并安装了应用程序,在启动时很有可能在键入它的名称时出现"command not found"的提示内容.如果每次都到安装目标文件夹内,找到可执行文件来进 ...
- linux关于profile 、bashrc 、.bash_profile、.bashrc的区别
linux关于profile .bashrc ..bash_profile..bashrc的区别 - /etc/profile /etc/bashrc ~/.bash_profile ~/.bashr ...
- .bash_profile和.bashrc的区别,ubuntu下为.profile,没有.bash_profile
.bash_profile 开机自动加载,比如java的环境变量放在里面 .bashrc打开shell或终端就会加载该文件,比如起的别名或快捷方式放里面.alias设置就在其中. 还有一个.profi ...
随机推荐
- html冲刺
html知识点回顾与面试题<!--1.<DOCTYPE>告诉浏览器当前文档要以何种HTML或者XHTML规范解析2.语义标签strong 粗体em 斜体del 删除线ins 下划线 ...
- log4j2自定义Appender(输出到文件/RPC服务中)
1.背景 虽然log4j很强大,可以将日志输出到文件.DB.ES等.但是有时候确难免完全适合自己,此时我们就需要自定义Appender,使日志输出到指定的位置上. 本文,将通过两个例子说明自定义APP ...
- SharePoint PowerShell 启动工作流
前言 最近,有个需求,就是需要批量更新列表内容,平时这些操作,都用powershell去完成,但是,powershell需要在服务器上操作,而服务器权限需要通过客户的层层把关,所以,这次就考虑不用命令 ...
- springboot拦截器@Autowired为null解决
问题原因 拦截器加载的时间点在springcontext之前,所以在拦截器中注入自然为null 文件解决 在spring配置文件中这样写 @Bean public HandlerInterceptor ...
- 《闲聊 ASP.NET Core》系列直播清单
[闲聊 ASP.NET Core]第一期:项目与应用结构 [闲聊 ASP.NET Core]第二期:Web Host 初始化与生命周期事件 [闲聊ASP.NET Core]第三期:应用程序配置 [闲聊 ...
- ionic ion-tab图标修改, 自定义tab图标
遇到需要自定义tab图标很简单, 只需要自定义相应的css就可以了, 这里要注意的是如何调整背景图片的大小. <ion-view hide-back-button="false&quo ...
- 用分离、附加的方式实现sql server数据库的备份和还原
一.数据库分离.附加的说明 SQL Server提供了"分离/附加"数据库."备份/还原"数据库.复制数据库等多种数据库的备份和恢复方法.这里介绍一种学习中常用 ...
- manjaro折腾手记
以前装过Arch,有点折腾,写了个hello world就卸载了.没用过AUR,甚至也没去了解. 听说manjaro继承Arch,几乎开箱即用,对硬件支持非常好,源里面的软件更新非常快.但是没有装中文 ...
- 评分卡模型剖析之一(woe、IV、ROC、信息熵)
信用评分卡模型在国外是一种成熟的预测方法,尤其在信用风险评估以及金融风险控制领域更是得到了比较广泛的使用,其原理是将模型变量WOE编码方式离散化之后运用logistic回归模型进行的一种二分类变量的广 ...
- C#语法——泛型的多种应用 C#语法——await与async的正确打开方式 C#线程安全使用(五) C#语法——元组类型 好好耕耘 redis和memcached的区别
C#语法——泛型的多种应用 本篇文章主要介绍泛型的应用. 泛型是.NET Framework 2.0 版类库就已经提供的语法,主要用于提高代码的可重用性.类型安全性和效率. 泛型的定义 下面定义了 ...