bashrc,bash_profile等shell配置文件详解
一、bashrc,bash_profile等文件执行顺序 如何确定这些文件的执行顺序呢? 最好的方法就是在这些文件的开头添加一个输出语句,这样文件一执行马上就会输出内容。
【bashrc,bash_profile等shell配置文件详解】我们首先在/etc/profile,/etc/bashrc,~/.bash_profile,~/.bashrc,~/.bash_logout文件开头添加以下行:
1 | echo
"I am `pwd`/`basename $0` ,executed at `date`." |
为什么不添加到文件尾部?因为这些文件可以执行,在执行过程中会调用其它文件,如果以上语句添加到文件尾部,则可能被调用的文件好像在前面执行,这就会发生错误。
修改/tmp/bashseq文件权限, 使所有用户可写:
1 | chmod
666
/tmp/bashseq |
1 2 3 4 | I am /etc/profile ,exectued at Wed May 11 10:23:18 CST 2011. I am ~/.bash_profile ,exectued at Wed May 11 10:23:19 CST 2011. I am ~/.bashrc ,exectued at Wed May 11 10:23:19 CST 2011. I am /etc/bashrc ,exectued at Wed May 11 10:23:19 CST 2011. |
如下图所示:
将当前用户logout,重新login, 再查看/tmp/bashseq文件,内容如下:
1 2 3 4 5 | I am ~/.bash_logout ,executed at Wed May 11 10:24:03 CST 2011. I am /etc/profile ,exectued at Wed May 11 10:25:50 CST 2011. I am ~/.bash_profile ,exectued at Wed May 11 10:25:50 CST 2011. I am ~/.bashrc ,exectued at Wed May 11 10:25:50 CST 2011. I am /etc/bashrc ,exectued at Wed May 11 10:25:50 CST 2011. |
二、各个文件作用 清空/tmp/bashseq文件, 使用普通用户登录, 查看/tmp/bashseq文件,内容如下:
1 2 | I am /etc/profile ,exectued at Wed May 11 10:24:12 CST 2011. I am /etc/bashrc ,exectued at Wed May 11 10:24:12 CST 2011. |
由此可见,上面四个文件/etc/profile,/etc/bashrc,~/.bash_profile,~/.bashrc中:
- 前两件是全局的,每一个用户登录都会执行
- 后两个是专用的,针对特定用户设定.
- .bash_profile内容:
1 2 3 4 5 6 7 8 9 | # .bash_profile # Get the aliases and functions if
[ -f ~/.bashrc ];
then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME
/bin export
PATH unset
USERNAME |
- .bashrc内容:
1 2 3 4 5 6 7 8 9 10 | # .bashrc # User specific aliases and functions alias
rm
=
'rm -i' alias
cp
=
'cp -i' alias
mv
=
'mv -i' # Source global definitions if
[ -f
/etc/bashrc
];
then .
/etc/bashrc fi ##end |
三、总结 讲到这里,你应该知道了如何对bashrc,bash_profile,bash_profile进行解析了。这些文件的界限并非十分明确,但是用户和全局必须分清。掌握这些内容,有助于编写shell script,也有助于对系统进行配置。
四、参考文章
- .bash_profile和.bashrc的什么区别
推荐阅读
- Shell-Bash变量与运算符
- 误删/清空.bashrc文件/内容
- 一键编译部署Mysql
- 7/25|7/25 day2- Bash shell
- GitHub秘钥配置
- iOS|iOS Xcode统一修改RGB的color profile
- Alpine Docker 安装 bash
- while循环打印
- Spring|Spring Boot实战(二)Spring常用配置 2.4 Profile
- Windows|Windows 环境下 git 客户端中的 git-bash 和 MINGW64