/etc/profile, ~/.bash_profile, ~/.bashrc, ~/.profile小结

一、概念理解

profile】从概念上来说,profile一般指"个人资料",这个一般是跟用户相关的,因此,/etc/profile, ~/.profile, ~/.bash_profile这三个都是跟登录相关的。

bashrc】我特意查了一下,这里的rc通常指run commands,也有些是说run control,我个人觉得run control更准确。

~/】这个是指当前登录用户下的个人配置

/etc/profile】这个是指系统下的所有用户的全局配置

二、区别

1.~/.bashrc

  • 描述:这个文件是针对每个用户的交互式非登录bash shell的配置文件
  • 加载时机:每当用户打开一个新的交互式窗口(或标签页)时,~/.bashrc就会被读取并执行一次
  • 用途:通常用于设置个人的环境变量、别名定义、shell 函数以及其他个性化配置。
  • 示例内容
    # Source global definitions
    if [ -f /etc/bashrc ]; then
      . /etc/bashrc
    fi
    
    # User specific aliases and functions
    alias ll='ls -l'

2.~/.bash_profile

  • 描述:也是针对每个用户的 bash shell 的个人配置文件,但是它的加载优先级高于 ~/.bashrc
  • 加载时机:在用户登录系统时被读取和执行一次。
  • 用途:通常用于设置全局环境变量,例如 JAVA_HOMEPATH 等。
  • 示例内容
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
      . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
    export PATH=$JAVA_HOME/bin:$PATH

3.~/.profile

  • 描述:针对每个用户的个人配置文件,适用于所有 shell(不仅仅是 bash)。
  • 加载时机:在用户登录系统时被读取和执行一次。
  • 用途:通常用于设置适用于所有 shell 的环境变量。
  • 示例内容
    # The default umask is set in /etc/profile; for setting the umask
    # for ssh logins, install and configure the libpam-umask package.
    #umask 022
    
    # if running bash
    if [ -n "$BASH_VERSION" ]; then
      # include .bashrc if it exists
      if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
      fi
    fi
    
    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/bin" ] ; then
      PATH="$HOME/bin:$PATH"
    fi

4./etc/profile

  • 描述:系统范围内的配置文件,适用于所有用户和所有 shell(不仅仅是 bash)。
  • 加载时机:在用户登录系统时被读取和执行一次。
  • 用途:通常用于设置系统范围内的环境变量和其他全局配置。

三、总结


~/.bashrc:适用于每个用户的 bash shell,每次打开新的终端窗口时加载。


~/.bash_profile:适用于每个用户的 bash shell,登录时加载,可以包含 ~/.bashrc。


~/.profile:适用于每个用户的所有 shell,登录时加载。


/etc/profile:适用于所有用户的系统范围配置,登录时加载。


根据需求,我们可以选择合适的地方来配置环境变量或其他个性化设置。通常,如果只需要配置 bash shell 的个性化设置,可以使用 ~/.bashrc;如果需要设置全局环境变量,可以使用 ~/.bash_profile 或 ~/.profile。系统级别的配置则放在 /etc/profile 中。


四、参考链接:

https://superuser.com/questions/173165/what-does-the-rc-in-bashrc-etc-mean

https://blog.csdn.net/Tracycoder/article/details/141819197

你可能感兴趣的:(1024程序员节)