CentOS7下的 /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 笔记2208300059

CentOS7下的 /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc

它们都是脚本, 都会在登录系统时(第一层shell(barh))执行
/etc/profile、~/.bash_profile 只会在每次登录系统(顶层shell)时执行一次
/etc/bashrc、~/.bashrc 在每次创建子bash时都会触发
/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 都可用来配置环境变量 , 但作用域不同
/etc/profile、/etc/bashrc 是系统级别的
~/.bash_profile、~/.bashrc 是用户级别的
/etc/bashrc 是被 ~/.bashrc 调用的
而 ~/.bashrc 是被 ~/.bash_profile 调用的
~/.bash_profile 调用 ~/.bashrc 调用 /etc/bashrc



/etc/profile

用来设置系统环境参数,比如$PATH
对所有用户生效的。

cat /etc/profile

sudo vi /etc/profile



/etc/bashrc

设置bash相关的内容,
对系统内所有用户生效。
只要用户运行bash命令,那么这里面的东西就在起作用。

cat /etc/bashrc

sudo vi /etc/bashrc



~/.bash_profile = $HOME/.bash_profile

用来设置一些环境变量,功能和/etc/profile 类似,作用域只针对当前用户
~波浪号代表用户在/Home目录下的主文件夹

cat ~/.bash_profile

vi ~/.bash_profile

cat $HOME/.bash_profile

vi $HOME/.bash_profile



~/.bashrc = $HOME/.bashrc

作用类似于/etc/bashrc, 只是针对当前用户,不对其他用户生效。
~波浪号代表用户在/Home目录下的主文件夹

cat ~/.bashrc

vi ~/.bashrc

cat $HOME/.bashrc

vi $HOME/.bashrc

$HOME/.bash_profile 调用 $HOME/.bashrc 调用 /etc/bashrc
CentOS7下的 /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 笔记2208300059_第1张图片
CentOS7下的 /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 笔记2208300059_第2张图片




对比 profile 和 bashrc

一个试验
在四个文件种分别加入 ------echo ‘该文件名’
然后登录系统,新开bash,新开sh
CentOS7下的 /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 笔记2208300059_第3张图片

可看出:

  1. 执行顺序是 /etc/profile , /etc/bashrc , ~/.bashrc , ~/.bah_profile
  2. 运行bash命令的时候 , 会触发 /etc/bashrc和~/.bashrc  ,  不会触发 /etc/profile和~/.bash_profile
  3. 运行sh命令的时候, 都不会触发
    CentOS7下的 /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 笔记2208300059_第4张图片
    可看出
    root登录时,会执行/etc/profile和/etc/bashrc , 不会执行z用户的.bashrc和.bash_profile




你可能感兴趣的:(OS操作系统,Windows,Linux,等,#,Linux,CentOS,Ubuntu,等,#,控制台命令行,Shell脚本,sh,cmd,等,linux,bash)