执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout
关于各个文件的作用域,在网上找到了以下说明:
(1)/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,并从/etc/profile.d目录的配置文件中搜集shell的设置。
(2)/etc/bashrc:为每一个运行bash shell的用户执行此文件。当bash shell被打开时,该文件被读取。
(3)~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。
(4)~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。
(5)~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件;另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承 /etc/profile中的变量,他们是"父子"关系。
~/.bash_profile是交互式、login 方式进入 bash 运行的
~/.bashrc 是交互式 non-login 方式进入 bash 运行的通常二者设置大致相同,所以通常前者会调用后者。
/etc/profile和/etc/environment等各种环境变量设置文件的用处
先将export LANG=zh_CN加入/etc/profile ,退出系统重新登录,登录提示显示英文。
将/etc/profile 中的export LANG=zh_CN删除,将LNAG=zh_CN加入/etc/environment,退出系统重新登录,登录提示显示中文。
用户环境建立的过程中总是先执行/etc/profile然后在读取/etc/environment。为什么会有如上所叙的不同呢?
应该是先执行/etc/environment,后执行/etc/profile。
/etc/environment是设置整个系统的环境,而/etc/profile是设置所有用户的环境,前者与登录用户无关,后者与登录用户有关。
系统应用程序的执行与用户环境可以是无关的,但与系统环境是相关的,所以当你登录时,你看到的提示信息,象日期、时间信息的显示格式与系统环境的LANG是相关的,缺省LANG=en_US,如果系统环境LANG=zh_CN,则提示信息是中文的,否则是英文的。
对于用户的SHELL初始化而言是先执行/etc/profile,再读取文件/etc/environment.对整个系统而言是先执行/etc/environment。这样理解正确吗?
/etc/enviroment --> /etc/profile --> $HOME/.profile -->$HOME/.env (如果存在)
/etc/profile 是所有用户的环境变量
/etc/enviroment是系统的环境变量
登陆系统时shell读取的顺序应该是
/etc/profile ->/etc/enviroment -->$HOME/.profile -->$HOME/.env
原因应该是jtw所说的用户环境和系统环境的区别了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands from the file /etc/profile,
if
that file
exists
. After reading
that file, it looks
for
~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists
and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.
When a login shell exits, bash reads and executes commands from the
file ~/.bash_logout,
if
it
exists
.
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc,
if
that file
exists
. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to
read
and execute commands from file instead of
~/.bashrc.
When bash is started non-interactively, to run a shell script,
for
example, it looks
for
the variable BASH_ENV in the environment, expands
its value
if
it appears there, and uses the expanded value as the name
of a file to
read
and execute. Bash behaves as
if
the following com-
mand were executed:
if
[ -n
"$BASH_ENV"
]; then .
"$BASH_ENV"
; fi
but the value of the PATH variable is not used to search
for
the file
name.
If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible,
while
conforming to the POSIX standard as well. When invoked as an interac-
tive login shell, or a non-interactive shell with the --login option,
it first attempts to
read
and execute commands from /etc/profile and
~/.profile, in that order. The --noprofile option may be used to
inhibit this behavior. When invoked as an interactive shell with the
name sh, bash looks
for
the variable ENV, expands its value
if
it is
defined
, and uses the expanded value as the name of a file to
read
and
execute. Since a shell invoked as sh does not attempt to
read
and exe-
cute commands from any other startup files, the --rcfile option has
no
effect. A non-interactive shell invoked with the name sh does not
attempt to
read
any other startup files. When invoked as sh, bash
enters posix mode after the startup files are
read
.
When bash is started in posix mode, as with the --posix command line
option, it follows the POSIX standard
for
startup files. In this mode,
interactive shells expand the ENV variable and commands are
read
and
executed from the file whose name is the expanded value. No other
startup files are
read
.
Bash attempts to determine when it is being run by the remote shell
daemon, usually rshd. If bash determines it is being run by rshd, it
reads and executes commands from ~/.bashrc,
if
that file
exists
and is
readable. It will not
do
this
if
invoked as sh. The --norc option may
be used to inhibit this behavior, and the --rcfile option may be used
to force another file to be
read
, but rshd does not generally invoke
the shell with those options or allow them to be specified.
If the shell is started with the effective user (group) id not equal to
the real user (group) id, and the -p option is not supplied,
no
startup
files are
read
, shell functions are not inherited from the environment,
the SHELLOPTS variable,
if
it appears in the environment, is ignored,
and the effective user id is set to the real user id. If the -p option
is supplied at invocation, the startup behavior is the same, but the
effective user id is not
reset
.
|