在刚登陆Linux时,首先启动/etc/profile文件,之后再启动用户目录下的~/.bash_profile、~/.bash_login、~/.profile文件中的一个,
执行的次序为:~/.bash_profile、~/.bash_login、~/.profile
假如~/.bash_profile文件存在的话,通常都会执行~/.bashrc以及/etc/bashrc文件。
由于在~/.bash_profile文件中通常会有下边的代码:
vim~/.bash_profile
# Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi
~/.bashrc中,通常都会有以下代码:
vim~/.bashrc
# Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi
所以,~/.bashrc会调用/etc/bashrc文件。
最后,在退出shell时,就会执行~/.bash_logout文件。
执行次序为:
/etc/profile->(~/.bash_profile|~/.bash_login|~/.profile)->~/.bashrc->/etc/bashrc->~/.bash_logout
关于各个文件的作用域,在网上找到了以下说明:
1)/etc/profile
此文件为系统的每位用户设置环境信息和启动程序red hat linux下载,当用户第一次登陆时,该文件被执行.并从/etc/profile.d/目录的配置文件中搜集shell的设置。其配置对所有登陆的用户都有效。当被更改时,必须重启才能生效。英语描述:”Systemwideenvironmentandstartupprograms,forloginsetup.”。另外/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能承继/etc/profile中的变量,她们是"母子"关系。
2)~/.bash_profile
为当前用户设置专属的环境信息和启动程序,每位用户都可使用该文件输入专用于自己使用的shell信息,当用户登入时,该文件仅仅执行一次。默认情况下,设置一些环境变量linux端口映射,执行用户的.bashrc文件。理念类似于/etc/profile,只不过只对当前用户有效,也须要重启才会生效。注意:Centos7系统命名为.bash_profile,其他系统可能是.bash_login或.profile
3)~/.bashrc
该文件包含专用于你的bashshell的bash信息,当登陆时以及每次打开新的shell时,该该文件被读取执行。理念类似于/etc/bashrc,只不过只对当前用户有效,不须要重启,只须要打开新的shell即可生效。
4)/etc/bashrc
为每一个运行bashshell的用户执行此文件,当bashshell打开时,该文件被执行,其配置对所有使用bash的用户打开的每位bash都有效。当被更改后,不用重启只须要打开一个新的bash即可生效。英语描述:”Systemwidefunctionsandaliases.”
5)~/.bash_logout
当每次退出系统(退出bashshell)时,执行该文件,可以把一些清除工作的命令放进这个文件。
# cat ~/.bash_logout # ~/.bash_logout
6)~/.bash_history
保留了近来1000条bashshell执行的历史命令记录,最大保存1000条,且是先前注销前近来的1000条记录,查看历史记录条数echo$HISTSIZE,默认是1000,在配置文件/etc/profile文件中配置,为了安全可以降低历史记录条数为300,即更改vim/etc/profile里的变量HISTSIZE=300,或则为了降低记录条数,更改其HISTSIZE=10000
7)/etc/profile.d/
此文件夹里是除/etc/profile之外其他的”application-specificstartupfiles”。英语描述为”The/etc/profilefilesetstheenvironmentvariablesatstartupoftheBashshell.The/etc/profile.ddirectorycontainsotherscriptsthatcontainapplication-specificstartupfiles,whicharealsoexecutedatstartuptimebytheshell.”同时,这种文件”areloadedvia/etc/profilewhichmakesthemapartofthebash“profile”inthesamewayanyway.”因此可以简单的理解为是/etc/profile的一部份,只不过按类别或功能拆分成若干个文件进行配置了(便捷维护和理解)
# ll /etc/profile.d/ total 60 -rw-r--r--. 1 root root 1127 Jun 20 2014 colorls.csh -rw-r--r--. 1 root root 1143 Jun 20 2014 colorls.sh -rw-r--r--. 1 root root 92 Nov 22 2013 cvs.csh -rw-r--r--. 1 root root 78 Nov 22 2013 cvs.sh -rw-r--r-- 1 root root 192 Sep 10 2014 glib2.csh -rw-r--r-- 1 root root 192 Sep 10 2014 glib2.sh -rw-r--r--. 1 root root 1741 Jul 10 2014 lang.csh -rw-r--r--. 1 root root 2706 Jul 10 2014 lang.sh -rw-r--r--. 1 root root 122 Feb 7 2007 less.csh -rw-r--r--. 1 root root 108 Feb 7 2007 less.sh -rw-r--r-- 1 root root 976 Sep 24 2011 qt.csh -rw-r--r-- 1 root root 912 Sep 24 2011 qt.sh -rw-r--r--. 1 root root 97 Apr 5 2012 vim.csh -rw-r--r--. 1 root root 269 Apr 5 2012 vim.sh -rw-r--r--. 1 root root 169 May 20 2009 which2.sh
注意事项
1)以上须要重启才会生效的文件,虽然可以通过source/etc/xxx暂时生效。
2)文件的执行次序为:当登陆Linux时,首先启动/etc/environment和/etc/profilelinux运行文件,之后启动当前用户目录下的/.bash_profile,执行此文件时通常会调用/.bashrc文件,而执行/.bashrc时通常会调用/etc/bashrc,最退后出shell时,执行/.bash_logout。
简单来说次序为:(登陆时)/etc/environment–>/etc/profile(以及/etc/profile.d/里的文件)–>~/.bash_profile–>(打开shell时)~/.bashrc–>/etc/bashrc–>(退出shell时)~/.bash_logout
/etc/profile和/etc/environment等各类环境变量设置文件的好处
1)先将exportLANG=zh_CN加入/etc/profile,退出系统重新登陆,登陆提示显示中文。
2)先将/etc/profile中的exportLANG=zh_CN删掉,将LNAG=zh_CN加入/etc/environment,退出系统重新登陆,登陆提示显示英文。
用户环境完善的过程中总是先执行/etc/profile,之后再读取/etc/environment。为何会有如上所叙的不同呢?而不是先执行/etc/environment,后执行/etc/profile呢?
这是由于:/etc/environment是设置整个系统的环境linux运行文件,而/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
缘由应当是用户环境和系统环境的区别了,若果同一个变量在用户环境(/etc/profile)和系统环境(/etc/environment)有不同的值,那应当是以用户环境为准了。
CentOS7系统中的/etc/profile不生效
/etc/profile文件是每一个用户登入时,操作系统首先执行的文件,之后再将控制权交给用户目录下的~/.bashrc文件
~/.bash_profile文件是当前每位用户都可使用该文件输入专用于自己使用的shell信息,当用户登入时,该文件仅仅执行一次。默认情况下,设置一些环境变量,执行用户的.bashrc文件。理念类似于/etc/profile,只不过只对当前用户有效,也须要重启才会生效。
执行次序为:(登陆时)/etc/environment–>/etc/profile(以及/etc/profile.d/里的文件)–>~/.bash_profile–>(打开shell时)~/.bashrc–>/etc/bashrc–>(退出shell时)~/.bash_logout
为此,CentOS7系统中,在配置/etc/profile文件中设置的环境变量不生效时,可以考虑把环境变量设置到~/.bash_profile或~/.bashrc文件中
1)把环境变量设置到~/.bash_profile
# cat ~/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH #export PATH=/usr/local/nginx/sbin:/usr/local/php/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin export PATH=/usr/local/bin:$PATH export PHP_ROOT=/usr/local/php export PATH=$PHP_ROOT/bin:$PATH export NGINX_ROOT=/usr/local/nginx export PATH=$NGINX_ROOT/sbin:$PATH cd ~/script/
2)把环境变量设置到~/.bashrc
# cat ~/.bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi #export PATH=/usr/local/nginx/sbin:/usr/local/php/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin export PATH=/usr/local/bin:$PATH export PHP_ROOT=/usr/local/php export PATH=$PHP_ROOT/bin:$PATH export NGINX_ROOT=/usr/local/nginx export PATH=$NGINX_ROOT/sbin:$PATH
参考推荐:
Linux/bin,/sbin,/usr/bin,/usr/sbin区别
Linuxcrond不执行缘由剖析
Linuxshell局部变量与全局变量
CentOS搭建SVNServer
在命令行快速切换目录
原文:Linux之/etc/profile、~/.bash_profile等几个文件的执行过程