linux中的配置文件加载

.bashrc是home目录下的一个shell文件,用于储存用户的个性化设置。在bash每次启动时都会加载.bashrc文件中的内容,并根据内容定制当前bash的配置和环境。

补充: .bash_profile和.bashrc的区别?
两者在登陆bash时都会被bash执行,但是.bash_profile只在会话开始时被读取,而.bashrc在每次打开新的终端时都会被读取。

首先,.bashrc可以删除,但是删除不存在任何好处。
如果用户删除了.bashrc,可以从如下路径拷贝一份原始的.bashrc文件到用户home目录下

cp /etc/skel/.bashrc ~/

在/etc/profile写入NAME="zs"

[root@hecs-98663 ~]# echo $NAME

[root@hecs-98663 ~]# vim /etc/profile
[root@hecs-98663 ~]# echo $NAME

[root@hecs-98663 ~]# source /etc/profile
[root@hecs-98663 ~]# echo $NAME
zs
[root@hecs-98663 ~]# bash
[root@hecs-98663 ~]# echo $NAME

在.bash_profile写入NAME="zs"

[root@hecs-98663 ~]# echo $NAME

[root@hecs-98663 ~]# vim .bash_profile 
[root@hecs-98663 ~]# echo $NAME

[root@hecs-98663 ~]# source .bash_profile 
[root@hecs-98663 ~]# echo $NAME
zs
[root@hecs-98663 ~]# bash
[root@hecs-98663 ~]# echo $NAME

在.bashrc写入NAME="zs"

[root@hecs-98663 ~]# echo $NAME

[root@hecs-98663 ~]# vim .bashrc
[root@hecs-98663 ~]# echo $NAME

[root@hecs-98663 ~]# source .bashrc
[root@hecs-98663 ~]# echo $NAME
zs
[root@hecs-98663 ~]# bash
[root@hecs-98663 ~]# echo $NAME
zs

在sh中尝试

[root@hecs-98663 ~]# sh
sh-4.2# echo $NAME

sh-4.2# exit
exit
[root@hecs-98663 ~]# bash
[root@hecs-98663 ~]# echo $NAME
zs

“profile”与“rc”系列

通过名字的不同,我们可以直观地将startup文件分为“profile”与“rc”两个系列,其实他们的功能都很类似,但是使用的场景不同,这也是大家最容易忽略的地方。

所谓的不同场景,其实就是shell的运行模式。我们知道运行中的bash有“交互”和“登陆”两种属性,而执行“profile”系列还是“rc”系列,就与shell的这两个属性有关。
原理上讲,“登陆shell”启动时会加载“profile”系列的startup文件,而“交互式非登陆shell”启动时会加载“rc”系列的startup文件。

posted @ 2023-07-15 23:13  Bre-eZe  阅读(141)  评论(0)    收藏  举报