bashrc profile 相关的几个文件

0.相关文件

/etc/bash.bashrc
/etc/profile
~/.bashrc
~/.profile

另外还会有人提到~/.bash_profile
这个文件和~/.profile 是差不多的,区别只在于~/.profile 兼容性更好,而~/.bash_profile 是bash专用1


1.文件被使用的时机

在上面提到的四个文件最后加入echo $BASH_SOURCE 2,然后登陆系统,并且键入bash 以启用子bash

Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-91-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Last login: Sat Apr 21 21:49:44 2018 from xx.xx.xx.xx

/etc/bash.bashrc
/etc/profile
~/.bashrc
~/.profile

ubuntu@VM-0-5-ubuntu:~$ bash
/etc/bash.bashrc
~/.bashrc

2.文件间相互调用的关系:

~/.profile 文件调用了~/.bashrc

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

/etc/.profile 文件同调用了/etc/bash.bashrc

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\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

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

另外可以看到/etc/profile 还遍历执行了文件夹/etc/profile.d中的脚本


3.总结

  • 用户登陆只执行/etc/profile~/.profile ,比如ssh登陆
  • 新进的bash只执行/etc/bash.bashrc~/.bashrc ,比如键入bash

  • 系统的环境变量应该放在/etc/bash.bashrc

  • 用户专属的环境变量该放在~/.bashrc

不过依然不明白~/.profile这个东西有什么用(非终端操作的环境?桌面系统吗?)

猜你喜欢

转载自blog.csdn.net/harryhare/article/details/80034351