• Linux下的 /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
    
    
    • 1
    • 2
    sudo vi /etc/profile
    
    
    • 1
    • 2



    /etc/bashrc

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

    cat /etc/bashrc
    
    
    • 1
    • 2
    sudo vi /etc/bashrc
    
    
    • 1
    • 2



    ~/.bash_profile = $HOME/.bash_profile

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

    cat ~/.bash_profile
    
    
    • 1
    • 2
    vi ~/.bash_profile
    
    
    • 1
    • 2
    cat $HOME/.bash_profile
    
    
    • 1
    • 2
    vi $HOME/.bash_profile
    
    
    • 1
    • 2



    ~/.bashrc = $HOME/.bashrc

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

    cat ~/.bashrc
    
    
    • 1
    • 2
    vi ~/.bashrc
    
    
    • 1
    • 2
    cat $HOME/.bashrc
    
    
    • 1
    • 2
    vi $HOME/.bashrc
    
    
    • 1
    • 2

    $HOME/.bash_profile 调用 $HOME/.bashrc 调用 /etc/bashrc
    在这里插入图片描述
    在这里插入图片描述




    对比 profile 和 bashrc

    一个试验
    在四个文件种分别加入 ------echo ‘该文件名’
    然后登录系统,新开bash,新开sh
    在这里插入图片描述

    👆可看出:

    1. 执行顺序是 /etc/profile , /etc/bashrc , ~/.bashrc , ~/.bah_profile
    2. 运行bash命令的时候 , 会触发 /etc/bashrc和~/.bashrc  ,  不会触发 /etc/profile和~/.bash_profile
    3. 运行sh命令的时候, 都不会触发
      在这里插入图片描述
      👆可看出
      root登录时,会执行/etc/profile和/etc/bashrc , 不会执行z用户的.bashrc和.bash_profile




  • 相关阅读:
    【docker】docker 、docker-compose离线安装
    AI 绘画极简教程
    程序员如何学习开源项目,这篇文章告诉你
    国庆10.01
    【入门篇】1.7 Redis 之 codis 入门介绍
    2023年IB生物有什么变化?
    Python 随机字符串的生成方式
    简易实现Spring Framework底层机制
    简单神经网络模型怎么做,简单神经网络模型制作
    【EI会议2023】12.20之后ddl
  • 原文地址:https://blog.csdn.net/kfepiza/article/details/126591299