首先先总体看一下区别
============
/etc/profile
此文件为系统的每个用户设置环境信息,对所有用户有效
===========
/etc/bashrc (ubuntu为 /etc/bash.bashrc)
为每一个运行bash shell的用户执行此文件.对所有用户有效
===============
~/.bash_profile (ubuntu为 ~/.profile)
类似/etc/profile,但仅仅针对当前用户有效
=========
~/.bashrc
类似/etc/bashrc,但仅仅针对当前用户有效
==============
~/.bash_logout
当每次退出系统(退出bash shell)时,执行该文件.
- Linux的Shell种类众多,常见的有:
Bourne Shell(/usr/bin/sh或/bin/sh)、
Bourne Again Shell(/bin/bash)、
C Shell(/usr/bin/csh)、
K Shell(/usr/bin/ksh)、
Shell for Root(/sbin/sh)等等。- 不同的Shell语言的语法有所不同,所以不能交换使用。每种Shell都有其特色之处,基本上,掌握其中任何一种 就足够了。在本文中,我们关注的重点是Bash,也就是Bourne Again Shell,由于易用和免费,Bash在日常工作中被广泛使用;同时,Bash也是大多数Linux系统默认的Shell。
login
和non login
指的是用登录或非登录的方式打开bash shell,不同的方式的读取的配置文件不同,可以归纳为下表:
login | non login | |
---|---|---|
全局 | /etc/profile | /etc/bashrc |
单用户 | ~/.bash_profile | ~/.bashrc |
登录Linux时执行
在 刚登录Linux时,首先启动 /etc/profile
文件,然后再启动用户目录下的 ~/.bash_profile
再执行用户的bash设置:
如果 ~/.bash_profile
文件存在的话,会执行用户的 ~/.bashrc
文件。
#if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
同样~/.bashrc
中,一般还会在文件的前面有以下代码,来执行/etc/bashrc
if [ -f /etc/bashrc ] ; then
. /etc/bashrc
所以,~/.bashrc
会调用 /etc/bashrc
文件。最后,在退出shell时,还会执行 ~/.bash_logout
文件。
执行顺序为:
- /etc/profile
- ~/.bash_profile | ~/.bash_login | ~/.profile
- ~/.bashrc
- /etc/bashrc
- ~/.bash_logout
下面是几个例子:
/etc/profile
和~/.profile
/etc/bash.bashrc
和~/.bashrc
/etc/bash.bashrc
,/etc/profile
和~/.bash_profile
/etc/bash.bashrc
,/etc/profile
和~ /.bash_profile
。/etc/bash.bashrc
和~/.bashrc
~/.bash_logout
/etc/profile
和~/.bash_profile
,若使用其它方式,如:bash a.sh, ./a.sh,sh a.sh(这个不属于bash shell),则不会读取上面的任何文件。参考:linux下的/etc/profile、/etc/bashrc、/.bash_profile、/.bashrc文件
原文:https://www.cnblogs.com/acelin/p/15102801.html