关于“.bash_profile”和“.bashrc”区别的总结

bash的startup文件Linux shell是用户与Linux系统进行交互的媒介,而bash作为目前Linux系统中最常用的shell,它支持的startup文件也并不单一,甚至容易让人感到费解。本文以CentOS7系统为例,对bash的startup文件进行一些必要的梳理和总结。
摘要由CSDN通过智能技术生成

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文件。

交互式登陆

  • 34
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值