Linux进阶之环境变量文件/etc/profile、/etc/bashrc、/etc/environment

1、Centos 环境变量/etc/profile/etc/profile.dlinux

1、两个文件都是设置环境变量的文件。shell

2/etc/profile 是永久性的环境变量,是全局变量,/etc/profile.d/ 设置对全部用户生效vim

3/etc/profile.d/ /etc/profile 好维护。不想要什么变量直接删除/etc/profile.d/下对应的.sh shell脚本便可,不用像/etc/profile须要改动此文件。tomcat

区别:bash

/etc/profile 是文件less

1、只有Login shell 启动时才会运行 /etc/profile 这个脚本,而Non-login shell 不会调用这个脚本。spa

说明:当一个用户登陆Linux系统或使用su -命令切换到另外一个用户时,也就是Login shell 启动时,首先要确保执行的启动脚本就是 /etc/profile 操作系统

2、在/etc/profile 文件中设置的变量是全局变量。继承

/etc/profile.d 文件夹,下面包含不少.sh 脚本ci

1)在/etc/profile.d 目录中存放的是一些应用程序所需的启动脚本,而这些脚本文件是用来设置一些变量和运行一些初始化过程的。其中包括了颜色、语言、lessvimwhich等命令的一些附加设置。

2/etc/profile.d 下的脚本之因此能自动执行,是由于在/etc/profile 中有一个for循环语句来调用这些脚本。

环境变量配置

1/etc/profile配置

#################################

#在文件最后添加

export PATH=/usr/local/tomcat/bin/:$PATH

##################################

2/etc/profile.d配置

生成一个子配置文件tomcat.sh

###############################

export PATH=/usr/local/tomcat/bin/:$PATH

################################

2、/etc/profile/etc/bashrc的区别

/etc/profile:此文件为系统的每一个用户设置环境信息,当用户第一次登陆时,该文件被执行,并从/etc/profile.d目录的配置文件中搜集shell的设置。

/etc/bashrc:为每个运行bash shell的用户执行此文件,当bash shell被打开时,该文件被读取。

~/.bash_profile:每一个用户均可使用该文件输入专用于本身使用的shell信息,当用户登陆时,该文件仅仅执行一次!默认状况下,他设置一些环境变量,执行用户的.bashrc文件。是交互式login 方式进入 bash 运行的。

~/.bashrc:该文件包含专用于你的bash shellbash信息,当登陆时以及每次打开新的shell时,该文件被读取。交互式 non-login 方式进入 bash 运行的。

~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件。

另外,/etc/profile中设定的变量(全局)的能够做用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是\"父子\"关系。一般两者设置大体相同,一般前者会调用后者

3、在登陆Linux时要执行文件的过程以下:

在刚登陆Linux时,首先启动 /etc/profile 文件,而后再启动用户目录下的 ~/.bash_profile ~/.bash_login ~/.profile文件中的其中一个(根据不一样的linux操做系统的不一样,命名不同)

执行的顺序为:~/.bash_profile ~/.bash_login ~/.profile

若是 ~/.bash_profile文件存在的话,通常还会执行 ~/.bashrc文件由于在 ~/.bash_profile文件中通常会有下面的代码: if [ -f ~/.bashrc ] ; then . ./bashrc fi ~/.bashrc中,通常还会有如下代码: if [ -f /etc/bashrc ] ; then . /bashrc fi 因此,~/.bashrc会调用 /etc/bashrc文件。最后,在退出shell时,还会执行 ~/.bash_logout文件。

执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout

4、/etc/profile/etc/environment等各类环境变量设置文件的用处

先将export加入/etc/profile,退出系统从新登陆,登陆提示显示英文。

/etc/profile 中的export删除,将LNAG=zh_CN加入/etc/environment,退出系统从新登陆,登陆提示显示中文。

用户环境创建的过程当中老是先执行/etc/profile而后在读取/etc/environment为何会有如上所叙的不一样呢?

应该是先执行/etc/environment,后执行/etc/profile

/etc/environment是设置整个系统的环境,而/etc/profile是设置全部用户的环境,前者与登陆用户无关,后者与登陆用户有关。

系统应用程序的执行与用户环境可无关但与系统环境是相关的,对于用户的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)有不一样的值那应该是以用户环境为准了。

相关文章
相关标签/搜索