2024年最新etc bashrc和 etc profile傻傻分不清楚?_

/etc/profile
=> /etc/bash.bashrc

~/.profile
=> ~/.bashrc => /etc/bashrc



**终端种直接运行 bash**


/etc/bash.bashrc
~/.bashrc => /etc/bashrc



bash -c “CMD”
什么都不执行



ssh server “CMD”
/etc/bash.bashrc => /etc/profile
~/.bashrc => | /etc/bashrc => /etc/profile
| ~/.profile


这里会有点小混乱, 因为既有 /etc/bash.bashrc 又有 /etc/bashrc, 其实是这样的 ubuntu 和 debian 有 /etc/bash.bashrc 文件但是没有 /etc/bashrc, 其他的系统基本都是只有 /etc/bashrc 没有 /etc/bash.bashrc.



**最终修改**

为了达到上述我们需要的执行流程, 那必须对系统的 rc 文件做修改. 我们拿 Ubuntu 举例  
 首先 我们编辑 /etc/profile 在文件头部加入



export system_profile_loaded=1


这样其他文件就可以根据 $system\_profile\_loaded 来判断是否已经载入过 profile, 接着我们可以看到



unset i
fi

if [ “ P S 1 " ] ; t h e n i f [ " PS1" ]; then if [ " PS1"];thenif["BASH” ]; then
PS1=‘\u@\h:\w$ ’
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ “id -u” -eq 0 ]; then
PS1=’# ’
else
PS1='$ ’
fi
fi
fi

umask 022


按照我们刚才的方案, 应该是不管任何情况都应该在文件末尾去载入 bashrc, 所以我们修改成



unset i
fi

umask 022

if [ “ B A S H " ] ; t h e n i f [ " BASH" ]; then if [ " BASH"];thenif["PS1” ]; then
PS1='\u@\h:\w$ ’
fi

if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ “id -u” -eq 0 ]; then
PS1=‘# ’
else
PS1=’$ ’
fi
fi


当然也可以有其他该法,只要符合在文件末尾载入 bashrc 就可以了.  
 接着我们修改 /etc/bash.bashrc, 我们需要在文件头加入



[ -n “KaTeX parse error: Expected 'EOF', got '&' at position 30: …c\_running}" ] &̲& return system…{system_profile_loaded}” ] && source /etc/profile
unset system_bashrc_running
system_bashrc_runned=1


其中 system\_bashrc\_running 这样的变量都是为了防止2次反复调用而加入的.  
 这样系统级别的 rc 文件基本修改好了, 最好还可以再修改一下本地的rc文件, 所以我们编辑 “~/.profile”, 发现起内容是



~/.profile: executed by Bourne-compatible login shells.

if [ -n “$BASH” ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi

mesg n


而按照上述的修改规则只需要替换成



export local_profile_loaded=1

if [ -n “$BASH” ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi


这样就始终再载入完 profile 以后去载入 bashrc 了, 接着我们像编辑 /etc/bash.bashrc 一样的去修改 ~/.bashrc, 文件头上加入



[ -n “KaTeX parse error: Expected 'EOF', got '&' at position 29: …c\_running}" ] &̲& return local\…{system_bashrc_runned}” ] && source /etc/bashrc
[ -r ~/.profile -a -z “${local_profile_loaded}” ] && source ~/.profile
unset local_bashrc_running


用来防止反复加载 profile, 并且这里需要特殊注明的是



[ -r /etc/bashrc -a -z “${system_bashrc_runned}” ] && source /etc/bashrc


/etc/bashrc 这个文件只有在 mac 之类的系统下才有, 所以 ubuntu 这里这行可以不加, 不过有判断是否存在所以加了也没关系.  
 到这里基本上就可以比较完美的解决不通的 shell 加载顺序问题了, 当然比如这个用户用的是 zsh 之类的也需要按照类型的原理来修改.  
 另外, 在用户目录下 可能会存在 ~/.bash\_profile, ~/.bash\_login 这样的文件, 但如果有这些文件 bash 就不会去载入 ~/.profile 了, 所以如果存在的话需要删除 这些文件并把内容合并进 ~/.profile 和 ~/.bashrc 才行.


  
 


  
 



  
 


本文原创地址:<http://www.linuxprobe.com/diff-bashrcprofile.html>


免费提供最新Linux技术教程书籍,为开源技术爱好者努力做得更多更好:[http://www.linuxprobe.com/thread]( )




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值