执行顺序为:/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所说的用户环境和系统环境的区别了
当在Linux、Unix和Mac OS X下工作时,我总是我总是忘记要修改哪个bash配置文件来设置shell的PATH和其他的环境变量。是应该修改在home目录下的.bash_profile还是.bashrc文件?
.login是CShell(csh)的配置文件
.bash_profile是bash特有的,由于bash被设计成兼容sh,ksh,并有csh的特征,所以会在用户主目录依次查找.bash_profile,.bash_login,.profile并用找到的第一个作为自己的配置文件.
在Linux下sh是bash的一个链接,如果bash使用sh命令启动的,就会以sh兼容方式运行使用.profile
一下是部分manbash的输出中的有关部分:
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
|
Whenbashisinvokedasaninteractiveloginshell,orasanon-inter-
activeshellwiththe--loginoption,itfirstreadsandexecutescom-
mandsfromthefile/etc/profile,
if
thatfile
exists
.Afterreading
thatfile,itlooks
for
~/.bash_profile,~/.bash_login,and~/.profile,
inthatorder,andreadsandexecutescommandsfromthefirstonethat
exists
andisreadable.The--noprofileoptionmaybeusedwhenthe
shellisstartedtoinhibitthisbehavior.
Whenaloginshellexits,bashreadsandexecutescommandsfromthe
file~/.bash_logout,
if
it
exists
.
Whenaninteractiveshellthatisnotaloginshellisstarted,bash
readsandexecutescommandsfrom~/.bashrc,
if
thatfile
exists
.This
maybeinhibitedbyusingthe--norcoption.The--rcfilefileoption
willforcebashto
read
andexecutecommandsfromfileinsteadof
~/.bashrc.
Whenbashisstartednon-interactively,torunashellscript,
for
example,itlooks
for
thevariableBASH_ENVintheenvironment,expands
itsvalue
if
itappearsthere,andusestheexpandedvalueasthename
ofafileto
read
andexecute.Bashbehavesas
if
thefollowingcom-
mandwereexecuted:
if
[-n
"$BASH_ENV"
];then.
"$BASH_ENV"
;fi
butthevalueofthePATHvariableisnotusedtosearch
for
thefile
name.
Ifbashisinvokedwiththenamesh,ittriestomimicthestartup
behaviorofhistoricalversionsofshascloselyaspossible,
while
conformingtothePOSIXstandardaswell.Wheninvokedasaninterac-
tiveloginshell,oranon-interactiveshellwiththe--loginoption,
itfirstattemptsto
read
andexecutecommandsfrom/etc/profileand
~/.profile,inthatorder.The--noprofileoptionmaybeusedto
inhibitthisbehavior.Wheninvokedasaninteractiveshellwiththe
namesh,bashlooks
for
thevariableENV,expandsitsvalue
if
itis
defined
,andusestheexpandedvalueasthenameofafileto
read
and
execute.Sinceashellinvokedasshdoesnotattemptto
read
andexe-
cutecommandsfromanyotherstartupfiles,the--rcfileoptionhas
no
effect.Anon-interactiveshellinvokedwiththenameshdoesnot
attemptto
read
anyotherstartupfiles.Wheninvokedassh,bash
entersposixmodeafterthestartupfilesare
read
.
Whenbashisstartedinposixmode,aswiththe--posixcommandline
option,itfollowsthePOSIXstandard
for
startupfiles.Inthismode,
interactiveshellsexpandtheENVvariableandcommandsare
read
and
executedfromthefilewhosenameistheexpandedvalue.Noother
startupfilesare
read
.
Bashattemptstodeterminewhenitisbeingrunbytheremoteshell
daemon,usuallyrshd.Ifbashdeterminesitisbeingrunbyrshd,it
readsandexecutescommandsfrom~/.bashrc,
if
thatfile
exists
andis
readable.Itwillnot
do
this
if
invokedassh.The--norcoptionmay
beusedtoinhibitthisbehavior,andthe--rcfileoptionmaybeused
toforceanotherfiletobe
read
,butrshddoesnotgenerallyinvoke
theshellwiththoseoptionsorallowthemtobespecified.
Iftheshellisstartedwiththeeffectiveuser(group)idnotequalto
therealuser(group)id,andthe-poptionisnotsupplied,
no
startup
filesare
read
,shellfunctionsarenotinheritedfromtheenvironment,
theSHELLOPTSvariable,
if
itappearsintheenvironment,isignored,
andtheeffectiveuseridissettotherealuserid.Ifthe-poption
issuppliedatinvocation,thestartupbehavioristhesame,butthe
effectiveuseridisnot
reset
.
|