# Linux下环境变量的配置

最近一直在Linux下调试MQ，研究了一下Linux环境变量的配置，部分资料搜集自网上：

# 环境变量的配置文件

一般来说，许多Linux发行版的默认的Shell是Bash (GNU Bourne-Again Shell) ，Shell中变量根据作用域的不同（与大多数编程语言类似，比如在函数中定义的变量在函数外不能使用的)，分为局部变量和环境变量（全局变量）。局部变量是普通的变量，仅在创建它的Shell中有效。环境变量。我们更多的使用“环境变量”而不是“全局变量”，因为这个名称更能体现它的特点。环境变量对创建它的Shell及其派生出来的子进程都有效。或者说环境变量可以继承，但它对其它与创建它的Shell没有关系的Shell并不可见。其实重启一个终端就是重新启动了一个shell。

Linux的变量可分为两类：环境变量和本地变量。环境变量，或者称为全局变量，存在与所有的shell中，在你登陆系统的时候就已经有了相应的系统定义的环境变量了。Linux的环境变量具有继承性，即子shell会继承父shell的环境变量。#env 显示环境变量

本地变量，当前shell中的变量，很显然本地变量中肯定包含环境变量。Linux的本地变量的非环境变量不具备继承性。#set显示本地变量

对不同版本的Linux，其配置环境变量的文件可能有/etc/environment/etc/profile/etc/bashrc～/.profile~.bash_profile~/.bashrc~/.bash_logout ……

# 配置文件的说明

/etc/environment：是整个系统的环境，启动系统后就会去读取该文件，一般不需要改

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

# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

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

# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

~/.bash_profile
：每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.

~/.bashrc
：该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该文件被读取.（每个用户都有一个.bashrc文件，在用户目录下）

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

/etc/profile、/etc/bashrc 是系统全局环境变量设定 ；

~/.profile，~/.bashrc是用户目录下的私有环境变量设定；

/etc/profile中设定的变量(全局)的可以作用于任何用户；而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系。

# 配置文件的加载顺序

首先需要了解交互式shell和非交互式shell、登录shell和非登录shell的区别，这是按两个不同的维度来划分的，一个是是否交互式，另一个是是否登录。（这里参考

shell

login shell在某用户由/bin/login登陆进系统后启动的shell，跟这个用户绑定。这个shell是用户登陆后启动的第一个进程。login进程在启动shell时传递第0个參数指明shell的名字，该參数第一个字符为"-"，指明这是一个login shell比方对bash而言，启动參数为"-bash"。当bashlogin shell启动时，它会运行/etc/profile中的命令，然后/etc/profile调用/etc/profile.d文件夹下的全部脚本。然后运行~/.bash_profile~/.bash_profile调用~/.bashrc，最后~/.bashrc又调用/etc/bashrc要识别一个shell是否为login shell。仅仅需在该shell下运行echo $0:# echo$0。假设输出为该shell名字，加上一个'-'前缀。则说明该shelllogin shell比如-bash-su等等。

/etc/environment是设置整个系统的环境，而/etc/profile是设置所有用户的环境，前者与登录用户无关，后者与登录用户有关。系统应用程序的执行与用户环境可以是无关的，但与系统环境是相关的，所以当你登录时，你看到的提示信息，象日期、时间信息的显示格式与系统环境的LANG是相关的，缺省LANG=en_US，如果系统环境LANG=zh_CN，则提示信息是中文的，否则是英文的。如果同一个变量在/etc/profile和系/etc/environment有不同的值以/etc/profile为准。

，如RHEL6.3中非登录shell仅执行了“~/.bashrc”文件（没有执行/etc/bashrc），而KUbuntu10.04中却依次执行了/etc/bash.bashrc和 ~/.bashrc 文件。对于这些规则，可以直接在相应的配置文件中加一些echo命令来验证其真实性。

1./etc/profile/etc/profile.d/*

2. ~/.bash_profile~/.bashrc/etc/bashrc或者/etc/bash.bashrc
3.
4.
~/.profile

/etc/profile

1. ~/.bashrc/etc/bashrc或者/etc/bash.bashrc/etc/profile.d/*

# 配置环境变量的方法

//在最后一行添上：
export PATH=/usr/local/mongodb/bin:$PATH 生效方法：（有以下两种） 1、关闭当前终端窗口，重新打开一个新终端窗口就能生效 2、输入“source ~/.bashrc”命令，立即生效 有效期限：永久有效 用户局限：仅对当前用户 修改方法三: 通过修改/etc/profile/etc/bashrc文件: 如：vim /etc/profile /export PATH //找到设置PATH的行，添加 export PATH=/usr/local/mongodb/bin:$PATH

vim /etc/environment

# ubuntu 12.04中环境变量设置

Persistent environment variables

So far we've only discussed ways set an environment variable value temporarily until the shell session in which it was set is closed. One may wonder if there is a way to somehow permanently set an environment variable to a certain value.

Note: The shell config files mentioned below are only meant to be read by particular shells and desktop environments and should not be used.

## Session-wide environment variables

Environment variable settings that should affect just a particular user (rather than the system as a whole) should be set into:

• ~/.pam_environment - This file is specifically meant for setting a user's environment. It is not a script file, but rather consists of assignment expressions, one per line.

PATH DEFAULT=${PATH}:${HOME}/MyPrograms

Note: Using .pam_environment requires a re-login in order to initialize the variables. Restarting just the terminal is not sufficient to be able to use the variables.

If you are using KDE, see the KDE User-base page on this topic.

Not recommended anymore:

• ~/.profile - In this file you can also place environment variable assignments, since it gets executed automatically by the DisplayManager during the start-up process desktop session as well as by the login shell when one logs-in from the textual console.

• ~/.bash_profile or ~/.bash_login - If one of these file exist, bash executes it rather than "~/.profile" when it is started as a login shell. (Bash will prefer "~/.bash_profile" to "~/.bash_login"). However, these files won't influence a graphical session by default.

• ~/.bashrc - Because of the way Ubuntu currently sets up the various script files by default, this may be the easiest place to set variables in. The default configuration nearly guarantees that this file will be executed in each and every invocation of bash as well as while logging in to the graphical environment. The performance cost of this will be negligible; the overhead of forking and execing bash will massively dominate the small cost of setting a handful of variables.

## System-wide environment variables

Environment variable settings that affect the system as a whole (rather than just a particular user) should not be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded, but into

• /etc/environment - This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

Not recommended:

• /etc/profile - This file gets executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the DisplayManager when the desktop session loads. This is probably the file you will get referred to when asking veteran UNIX system administrators about environment variables. In Ubuntu, however, this file does little more than invoke the /etc/bash.bashrc file.

• /etc/bash.bashrc - This is the system-wide version of the ~/.bashrc file. Ubuntu is configured by default to execute this file whenever a user enters a shell or the desktop environment.

Note: Any variables added to these locations will not be reflected when invoking them with a sudo command, as sudo has a default policy of resetting the Environment and setting a secure path (this behavior is defined in /etc/sudoers). As a workaround, you can use "sudo su" that will provide a shell with root privileges but retaining any modified PATH variables.

Note: When dealing with end-user/home desktop systems may be appropriate to place settings in the user's ~/.pam_environment files discussed above rather than the system-wide ones, since those files do not require one to utilize root privileges in order to edit and are easily moved between systems.

Note: Some systems now use an envvar.sh placed in the /etc/profile.d/ directory to set system wide environment strings.

# Linux 下Shell变量,环境变量的联系与区别

Linux下Shell变量,环境变量的联系与区别

## 1、  简介

linux下的变量可分成两种：Shell变量和环境变量。

Shell变量，又称本地变量，包括私有变量以及用户变量，不同类的shell有不同的私有变量，bash,ksh,csh等，每种shell的私有变量都不一样

1.变量名和=号之间不能有空格，否则shell会把变量名当成命令

2.变量名称一般使用大写字母

## 2、  常用的相关命令

env命令：显示当前用户的环境变量

set命令：不加参数，显示当前shell所有本地设置的Shell变量，否则设置shell变量

unset命令：unset为shell内建指令，删除变量或函数。

exprot命令：显示(设置)当前导出成用户变量的shell变量(注意：export为bash或类bash私有的命令)

echo $变量名：查看变量值(其中，$变量：引用变量的意思)

## 3、  示例

a)  使用env命令显示当前用户的所有环境变量

[laiyu@localhost ~]$env HOSTNAME=localhost.localdomain SHELL=/bin/bash TERM=xterm HISTSIZE=1000 USER=laiyu LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;... MAIL=/var/spool/mail/laiyu PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/laiyu/bin PWD=/home/laiyu LANG=en_US.UTF-8 ... HOME=/home/laiyu LOGNAME=laiyu ... _=/bin/env b) 使用set命令显示当前shell所有本地Shell变量 [root@localhost ~]# set | more BASH=/bin/bash ... BASH_CMDS=() ... c) 使用unset命令来清除变量(不管是shell变量还是环境变量) [laiyu@localhost ~]$ export TEST=test #增加一个环境变量TEST

[laiyu@localhost ~]$env | grep TEST #查看增加的变量 TEST=test [laiyu@localhost ~]$ unset $TEST #删除环境变量TEST(这里TEST前有无美元符都一样) [laiyu@localhost ~]$ env | grep TEST #输出为空

d)  使用echo命令显示环境变量(HOME变量为例)

[root@localhost ~]# echo $HOME /root e) 使用export命令将shell变量导出为用户变量，并显示 列出所有的Shell赋予程序的环境变量 [root@localhost ~]# export -p #等同export ... declare -x HOME="/root" declare -x HOSTNAME="localhost.localdomain" declare -x LANG="en_US.UTF-8" declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s" declare -x LOGNAME="root" declare -xPATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/bin/:/usr/local/tsung/bin" ... 把shell变量导出为用户变量 [root@localhost ~]# SHOUKE=ishouke #定义shell变量 [root@localhost ~]# echo$SHOUKE

ishouke

#查看当前用户的用户变量是否存在SHOUKE变量，结果是不存在

[root@localhost ~]# env | grep SHOUKE

#查看当前shell的本地变量是否存在SHOUKE变量，结果是不存在

[root@localhost ~]# set | grep SHOUKE

SHOUKE=ishouke

#查看Shell赋予程序的环境变量中是否存在SHOUKE变量，结果不存在

[root@localhost ~]# export | grep SHOUKE

#导出shell变量为用户变量

[root@localhost ~]# export SHOUKE

[root@localhost ~]# env | grep SHOUKE

SHOUKE=ishouke

[root@localhost ~]#

--------------------------------------

[root@localhost ~]# export SHOUKE=ishouke

[root@localhost ~]# env | grep SHOUKE

SHOUKE=ishouke

[root@localhost ~]#

http://liuzhijun.iteye.com/blog/1744465

https://www.freebsd.org/cgi/man.cgi?query=bash&apropos=0&sektion=0

https://www.cnblogs.com/zhaofeng555/p/4895517.html

02-19 311

06-26 127
11-18 348
02-19 1008
01-20 440
04-23 7197