profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2
profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2
by:授客 QQ:1033553122
--------------------接Part 1-------------------
B. 当bash以interactive shell方式启动时:
如果存在~/.bashrc,bash从~/.bashrc中读取命令并执行
注意:
1.通过--rcfile file选项可强制bash从文件file中读取命令并执行,而不是从~/.bashrc中读取。
2.可通过使用--norc可禁止bash读取~/.bashrc中的内容
C. 当从login shell中注销登录时:
如果存在~/.bash_logout,bash会从~/.bash_logout中读取命令并执行
注意:interactive shell中执行退出是无法直接退出的,必须先退出到login shell
例:采用interactive shell中执行exit退出登录(因exit只会退到上次登录界面(如有的话))
[root@localhost ~]# echo $0
-bash #login shell
[root@localhost ~]# bash
[root@localhost ~]# echo $0
bash #interactive shell
[root@localhost ~]# exit
exit
[root@localhost ~]# exit #需要再次执行该命令才可以退出
[root@localhost ~]# who
root pts/0 2014-09-08 15:27 (172.25.75.2)
[root@localhost ~]# su -l
[root@localhost ~]# exit
logout
[root@localhost ~]# logout #需要再次执行才退出
D. sh script_name.sh一般情况下,不会执行上面的文件
E. 被远程shell守护进程运行会去读~/.bashrc中的命令
bash会探测自己是不是被远程shell守护程序运行(通常是rshd)。如果是,它会读取并执行~/.bashrc中的命令。但是rshd一般不会用rc相关参数调用shell,也不会允许指定这些参数
5、 实践检验理论
这里我们通过小实验来验证上述的结论是否正确
步骤1、备份~用户主目录下的文件
[test@localhost ~]$ pwd
/home/test
[test@localhost ~]$ cp .bash_profile bak.bash_profile
[test@localhost ~]$ cp .bashrc bak.bashrc
[test@localhost ~]$ cp .bash_logout bak.bash_logout
步骤2、新建.profile,.bash_login文件
[test@localhost ~]$ touch .bash_login
[test@localhost ~]$ touch .profile
步骤3、修改上述文件的内容
修改.bash_profile文件内容如下
echo "shouke test from ~/.bash_profile"
修改.bash_login文件内容如下
echo "shouke test from ~/.bash_login"
修改.profile文件内容如下
echo "shouke test from ~/.profile"
修改.bashrc文件内容如下
echo "shouke test from ~/.bashrc"
修改.bash_logout文件内容如下
echo "shouke test from ~/.bash_logout"
步骤4、修改/etc/profile和/etc/bashrc文件的内容
修改/etc/profile,在文件最末尾添加如下内容
echo 'shouke test from /etc/profile'
修改/etc/bashrc,在文件最末尾添加如下内容
echo 'shouke test from /etc/bashrc'
步骤5、运行测试
测试1.
字符界面下,在终端tty1~tty6中任意一个tty界面下,输入帐号,密码登录
测试2.
通过xshell工具远程连接系统
测试3.
字符界面下,运行命令su -、su –l、su –login
[root@localhost ~]# pwd
/root
#备份原来的配置文件并新建测试用配置文件
[root@localhost ~]# mv .bash_profile bak.bash_profile
[root@localhost ~]# mv .bashrc bak.bashrc
[root@localhost ~]# mv .bash_logout bak.bash_logout
[root@localhost ~]# cp /home/test/.bash_profile .bash_profile
[root@localhost ~]# cp /home/test/.bashrc .bashrc
[root@localhost ~]# cp /home/test/.bash_logout .bash_logout
[root@localhost ~]# cp /home/test/.profile .profile
[test@localhost ~]$ su -
Password:
shouke test from /etc/profile
shouke test from ~/.bash_profile
[test@localhost ~]$ su -l
shouke test from /etc/profile
shouke test from ~/.bash_profile
[test@localhost ~]$ su --login
Password:
shouke test from /etc/profile
shouke test from ~/.bash_profile
测试4.
可视桌面下,通过新建一个终端(Applications->System Tools->Terminal)
测试5.
已登录的情况下,在字符界面下,运行命令bash
-bash-4.1# bash
shouke test from ~/.bashrc
bash-4.1#
测试6.
root用户身份登录,在字符界面下,运行命令su、su username
-bash-4.1# su
shouke test from ~/.bashrc
bash-4.1# su test
shouke test from ~/.bashrc
普通用户身份登录,在字符界面下,运行命令su
-bash-4.1$ su
Password:
shouke test from ~/.bashrc
bash-4.1# echo $0
bash
测试6.
以root身份登录,登录后不做其它操作,直接注销登录
-bash-4.1# exit
logout
shouke test from ~/.bash_logout
以root身份登录,登录后运行bash命令,然后输入exit命令
-bash-4.1# bash
shouke test from ~/.bashrc
bash-4.1# exit
exit
以普通身份登录,登录后运行su -命令,然后输入exit命令
-bash-4.1$ su -
Password:
shouke test from /etc/profile
shouke test from ~/.bash_profile
-bash-4.1# exit
logout
shouke test from ~/.bash_logout
这里验证了上文说的,仅在login shell中,执行注销登录,才会读取~/.bash_logout
测试7.
新建test.sh文件,内容如下
#!/bin/bash
echo "shouke test from ~/test.sh"
login shell中
[test@localhost ~]$ sh test.sh
shouke test from ~/test.sh
interactive shell中
[test@localhost ~]$ bash
shouke test from ~/.bashrc
bash-4.1$ sh test.sh
shouke test from ~/test.sh
测试8.
以--rcfile参数跟随的方式启动bash
interactive shell
[test@localhost ~]$ bash --rcfile .bash_profile
shouke test from ~/.bash_profile
测试9.
以--norc参数跟随的方式启动bash
[root@localhost ~]# bash --norc
bash-4.1#
测试10.
去掉test用户的~/.bash_profile后,以test用户身份登录
[test@localhost ~]$ mv .bash_profile bak.last.bash_profile
登录结果:
Last login: Mon Sep 8 15:48:12 2014 from 172.25.75.2
shouke test from /etc/profile
shouke test from ~/.bash_login
测试11.
去掉test用户的~/.bash_login后,以test用户身份登录
[test@localhost ~]$ mv .bash_login bak.last.bash_login
登录结果:
Last login: Mon Sep 8 16:16:12 2014 from 172.25.75.2
shouke test from /etc/profile
shouke test from ~/.profile
测试12.
去掉test用户的~/.profile后,以test用户身份登录
[test@localhost ~]$ mv .profile bak.last.profile
登录结果:
Last login: Mon Sep 8 16:16:45 2014 from 172.25.75.2
shouke test from /etc/profile
说明:不同终端登录,有的还没退出,所以前缀符号不一样,有的是[test@localhost ~]$这种的,有的是-bash-4.1$这种的,但是不影响测试
作者:授客
微信/QQ:1033553122
全国软件测试QQ交流群:7156436
Git地址:https://gitee.com/ishouke
友情提示:限于时间仓促,文中可能存在错误,欢迎指正、评论!
作者五行缺钱,如果觉得文章对您有帮助,请扫描下边的二维码打赏作者,金额随意,您的支持将是我继续创作的源动力,打赏后如有任何疑问,请联系我!!!
微信打赏
支付宝打赏 全国软件测试交流QQ群
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库