Shell配置文件(脚本)的加载
与 Bash Shell 有关的配置文件主要有 /etc/profile、~/.bash_profile、~/.bash_login、~/.profile、~/.bashrc、/etc/bashrc、/etc/profile.d/*.sh,不同的启动方式会加载不同的配置文件。
~表示用户主目录。*是通配符,/etc/profile.d/*.sh 表示 /etc/profile.d/ 目录下所有的脚本文件(以.sh结尾的文件)。
登录式的 ShellBash 官方文档说:如果是登录式的 Shell,首先会读取和执行 /etc/profiles,这是所有用户的全局配置文件,接着会到用户主目录中寻找 ~/.bash_profile、~/.bash_login 或者 ~/.profile,它们都是用户个人的配置文件。
不同的 Linux 发行版附带的个人配置文件也不同,有的可能只有其中一个,有的可能三者都有,笔者使用的是 CentOS 7,该发行版只有 ~/.bash_profile,其它两个都没有。
如果三个文件同时存在的话,到底应该加载哪一个呢?它们的优先级顺序是 ~/.bash_profile ~/.bash_login ~/.profile。
如果 ~/.bash_profile 存在,那么一切以该文件为准,并且到此结束,不再加载其它的配置文件。
如果 ~/.bash_profile 不存在,那么尝试加载 ~/.bash_login。~/.bash_login 存在的话就到此结束,不存在的话就加载 ~/.profile。
注意,/etc/profiles 文件还会嵌套加载 /etc/profile.d/*.sh,请看下面的代码:
for i in /etc/profile.d/*.sh ; do if [ -r $i ]; then if [ ${-#*i} != $- ]; then . $i else . $i /dev/null done
同样,~/.bash_profile 也使用类似的方式加载 ~/.bashrc:
if [ -f ~/.bashrc ]; then . ~/.bashrc fi非登录的 Shell
如果以非登录的方式启动 Shell,那么就不会读取以上所说的配置文件,而是直接读取 ~/.bashrc。
~/.bashrc 文件还会嵌套加载 /etc/bashrc,请看下面的代码:
if [ -f /etc/bashrc ]; then . /etc/bashrc fi
22386.html
linux相关文章
- shell脚本语言(超全超详细)[通俗易懂]
- Shell脚本编程_shell简单脚本
- SHELL – shell 脚本获取本机ip并将ip复制给变量待用详解程序员
- Linux 查看 Shell 版本的方法(linux查看shell版本)
- 学习Linux玩转Shell脚本(linux编写shell脚本)
- Linux下执行Shell脚本的技巧(linux执行shell脚本)
- Linux当前Shell:从 Bash 到 Zsh(linux当前shell)
- 使用Shell脚本操作MySQL数据库(shell连接mysql)
- Linux脚本Shell编程:快速入门(linux脚本shell)
- 使用Shell实现MySQL监控:快速了解数据库状态!(shell监控mysql)
- 编程精通 Linux Shell 编程的必备技能(linux下shell)
- Linux 中最常用的Shell:为你提供便捷生活(linux常用shell)
- 命令深入了解Linux,学会常用Shell命令(linux常用shell)
- 脚本快速学习执行Linux Shell脚本的技巧(linux 执行shell)
- 一步一步学习:Shell脚本执行Linux命令初探(shell脚本执行linux命令)
- Linux下Shell脚本编程实践(linux的shell脚本)
- Linux定时自动运行Shell脚本,极大提升工作效率(linux定时执行shell脚本)
- 脚本Linux 中的 Shell 脚本:智能化自动化的提速器(linux中 shell)
- Linux下常见Shell Command使用技巧(linux常用shell)
- 让Oracle灵活的运行Shell脚本(oracle写shell)
- 快速清理Redis一键Shell操作(shell清空redis)
- 操作简单,通过Shell快速安装Redis服务(shell安装redis)
- Oracle Shell实现智能脚本管理(oracle shell)
- 脚本通过Redis获取Shell脚本的简单方法(redis获得shell)
- 解析php中如何直接执行SHELL