Ubuntu environment variable problem

When setting environment variables under linux, if you only use them temporarily, you can directly use the set or export command to set the environment variables under the shell. Manual setting, then you need to write the export command into a system file. The files with this function are as follows:
/etc/environment or /etc/profile or ~/.profile or /etc/bash. bashrc or ~/.bashrc etc.
There are so many available, what is the difference between them, and who comes first?

First, let's take a look at what these files do:
1./etc/environment–> is the first file that the system reads when logging in, and is used to set environment variables for all processes. When the system uses this file, it does not execute the commands in this file, but assigns VALUE to KEY according to the code in the KEY=VALUE mode. Therefore, if you want to define the PATH environment variable in the file, you only need to add a line like PATH=$PATH :/xxx/bin can be the code.
2./etc/profile–> is the second file executed when the system is logged in, which can be used to set environment variables for all users of the entire system.
3.~/.profile–> is the profile file corresponding to the currently logged in user, which is used to customize the personal working environment of the current user.
4./etc/bash.bashrc–> is the bash initialization file for all users. The environment variables set here will be applied to the shells of all users. This file will be executed every time the user opens the shell.
5.~/.bashrc–> is the bash initialization file corresponding to the currently logged-in user. When the user opens the shell, the system will execute this file once.

Then according to the above description, the order of execution of these files should be:
/etc/enviroment –>/etc/profile –>~/.profile –>/etc/bash.bashrc –> ~/.bashrc
In order to verify this order Is it correct? Here is a small experiment, assuming that the user name we log in is xyz. Add a line to /etc/environment:
ENV_MSG=”this is /etc/environment”
This is to add an environment variable ENV_MSG, and then add two lines of code to /etc/profile:
echo $ENV_MSG >> /home/xyz /log.txt
echo "this is /etc/profile" >> /home/xyz/log.txt
This way, if /etc/environment is read by the system before profile, in /home/xyz/log.txt it will be The value of EVN_MSG and the two messages of this is /etc/profile are printed out successively.
Add a line of code to /home/xyz/.profile:
echo “this is .profile” >> /home/xyz/log.txt
Add a line of code to /etc/bash.bashrc:
echo “this is /etc/bash .bashrc” >> /home/xyz/log.txt
Add a line of code to /home/xyz/.bashrc:
echo “this is .bashrc” >> /home/xyz/log.txt
Then, restart your computer and see what the log.txt file looks like.
After starting the computer, log in as user xyz and open /home/xyz/log.txt immediately, you can see the following three lines of messages in the file:
this is /etc/environment
this is /etc/profile
this is .profile
This means that the system is in During the process of starting the login, the contents of /etc/enviroment, /etc/profile and ~/.profile are read and executed in turn.
Then open a shell window, and two lines of messages will be added to the log file:
this is /etc/bash.bashrc
this is .bashrc
This means that during the process of opening the shell, the system executes /etc/bash.bashrc and ~/. bashrc. If you close the shell window and open a new shell window again, two more lines of the same message will be added to the log file. From this, it can be known that every time a new shell is opened, the system will repeatedly execute these two files without changing the contents of the first three files.
Next, we open /etc/environment, change the line just written to ENV_MSG=”this is not /etc/environment”, then log out, log in again with xyz, and we will find that there will be three more in the log file. Line:
this is not /etc/environment
this is /etc/profile
this is .profile
This also shows that logging out and logging in again will also cause the system to read and execute these three files.
However, if you press Ctrl+Alt+F1 and then log in to xyz, the following lines will appear in the log file. What's going on?
this is /etc/bash.bashrc
this is /etc/environment
this is /etc/profile
this is .bashrc
this is .profile

Reprint address: http://blog.sina.com.cn/s/blog_6405313801012pxw.html



There are three ways to set environment variables under Ubuntu, one for the current terminal, one for the current user, and one for all users :

 

 

One: for the current terminal:

Enter in the current terminal: export PATH=$PATH:<your path to be added>

However the above method only works on the current terminal, once the current terminal is closed or in another terminal, it will not work.

  export NDK_ROOT=/home/jiang/soft/android-ndk-r8e can only be used in the current terminal.

 

Two: for the current user:

There is a .bashrc hidden file in the user's home directory. The PATH settings can be added to this file as follows:

vi ~ / .bashrc

join in:

export PATH=<your path to be added>:$PATH

If you want to join multiple paths, just:

export PATH=<path1 you want to add>:<path2 you want to add>: ...... :$PATH

Each path is separated by a colon.

This will take effect every time you log in

 

 

Three: for all users:

sudo vi /etc/profile 

Add:
export PATH=<the path you want to add>:$PATH

That's it.

 

终端输入:echo $PATH 可以查看环境变量

 

 

注意,修改环境变量后,除了第一种方法立即生效外,第二第三种方法要立即生效,可以source ~/.bashrc或者注销再次登录后就可以了!

http://blog.csdn.net/z1002137615/article/details/50174567





系统级启动文件 

====================================

1、/etc/rc  主启动文件,不要修改它

2、/etc/rc.conf  决定启动哪些系统自带的守护进程,不要修改它

3、/etc/rc.conf.local 如果你想干涉系统启动时启动的守护进程,请编辑本文件,本文件的内容会覆盖/etc/rc.conf中的内容。

4、/etc/rc.local  重点,你想让Nginx,MySQL,Tomcat自启动,请修改这个文件。

5、/etc/rc.shutdown 重点,在操作系统关机时执行,可关闭Nginx,MySQL,Tomcat

 

1、 /etc/rc.local

系统启动需要加载的守护进程(服务)和执行的程序。该文件只有一条“exit 0”语句,用户可加入自己的启动程序,但需要保证最后的返回是0。

注意,该文件中不能加入用户shell下的脚本,例如加入 alias ll="ls -l" 无效,因为该文件的执行环境是在系统shell下,系统尚未进入用户shell环境。

可配置环境变量,可做开机自启动,因为开机自启动程序需要使用环境变量。

 

 

 

bash(用户级)启动文件 - [Ubuntu]  (/etc/profile 、/etc/bashrc 、 ~/.profile 、 ~/bashrc 文件)

====================================

1)进入系统时,先执行/etc/rc.local文件,再/etc/profile文件,再执行/etc/bashrc文件;结束后,进入bash(假如登陆用户为user),先执行/etc/profile文件,再执行/home/user/.bash_profile文件,根据该文件,执行/home/user/.bashrc文件,再根据该文件,执行/etc/bashrc文件,执行完毕后,整个执行过程结束。


2)切换用户(如从root用户切换至wxc用户)时,使用"su"命令,系统先执行/home/wxc/.bashrc文件,再根据该文件,执行/etc/bashrc文件,执行完毕后,整个执行过程结束;当使用"su -"命令时,先执行/etc/profile文件,再执行/etc/profile.d/*.sh和/home/wxc/.bash_profile文件,根据该文件,执行/home/wxc/.bashrc文件,再根据该文件,执行/etc/bashrc文件,执行完毕后,整个执行过程结束。

 

(1)/etc/profile

全局(公有)配置,不管是哪个用户,登录时都会读取该文件。

可配置环境变量,如JAVA_HOME

Mac 有此文件

 

(2)/etc/bashrc

全局(公有)配置,bash shell执行时,不管是何种方式,都会读取此文件。

Mac 有此文件 

Ubuntu没有此文件,与之对应的是/ect/bash.bashrc

 

 

(3)~/.bashrc

当bash shell是以non-login形式执行时,读取此文件。若是以login形式执行,则不会读取此文件。

当登录时以及每次打开新的shell时,该该文件被读取.

~/.bash_profile 与 ~/.bashrc  设置大致相同,所以通常前者会调用后者。

 

(4)~/.bash_profile

若bash shell是以login方式执行时,才会读取此文件。该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的~/.bashrc文件.

~/.bash_profile 与 ~/.bashrc  设置大致相同,所以通常前者会调用后者。

Mac 有此文件(注:Linux 里面是 .bashrc 而 Mac 是 .bash_profile)

Unbutu默认没有此文件,可新建。

 

(5)~/.bash_login

若bash shell是以login方式执行时,读取~/.bash_profile,若它不存在,则读取~/.bash_login,若前两者不存在,读取~/.profile。

 

(6)~/.profile

若bash shell是以login方式执行时,读取~/.bash_profile,若它不存在,则读取~/.bash_login,若前两者不存在,读取~/.profile。

另外,图形模式登录时,此文件将被读取,即使存在~/.bash_profile和~/.bash_login。

Mac 默认没有此文件,可手动创建此文件

 

 

(7)~/.bash_logout

注销时,且是longin形式,此文件才会读取。也就是说,在文本模式注销时,此文件会被读取,图形模式注销时,此文件不会被读取。

 

 

设置环境变量

export xxx=xxxx

例如 export http_proxy=http://example.com:8080

删除环境变量

unset xxxx

例如 unset http_proxy

 

 

下面是在本机(Ubuntu)的几个例子

====================================

1. 图形模式登录时,顺序读取:/etc/profile和~/.profile

2. 图形模式登录后,打开终端时,顺序读取:/etc/bash.bashrc和~/.bashrc

3. 文本模式登录时,顺序读取:/etc/bash.bashrc,/etc/profile和~/.bash_profile

4. 从其它用户su到该用户,则分两种情况:

(1)如果带-l参数(或-参数,--login参数),如:su -l username,则bash是lonin的,它将顺序读取以下配置文件:/etc/bash.bashrc,/etc/profile和~ /.bash_profile。

(2)如果没有带-l参数,则bash是non-login的,它将顺序读取:/etc/bash.bashrc和~/.bashrc

5. 注销时,或退出su登录的用户,如果是longin方式,那么bash会读取:~/.bash_logout

6. 执行自定义的shell文件时,若使用“bash -l a.sh”的方式,则bash会读取行:/etc/profile和~/.bash_profile,若使用其它方式,如:bash a.sh, ./a.sh,sh a.sh(这个不属于bash shell),则不会读取上面的任何文件。

7. 上面的例子凡是读取到~/.bash_profile的,若该文件不存在,则读取~/.bash_login,若前两者不存在,读取~/.profile。

http://www.cnblogs.com/yudar/p/4476029.html



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326425130&siteId=291194637