您的位置:首页 > 其它

Ubuntu 中到底如何设置环境变量

2015-01-11 21:34 513 查看
参考 : https://help.ubuntu.com/community/EnvironmentVariables
那么 在Ubuntu中怎样才是正确地设置环境变量的方法呢?

看官方建议如下:

一,System-wide environment variables

A suitable file for environment variable settings that affect the system as a whole (rather than just a particular user) is
/etc/environment. An alternative is to create a file for the purpose in the
/etc/profile.d directory.
上面说明了设置系统环境变量时的方法。即,在/etc/environment 文件中进行设置或者最好在/etc/profile.d/ 目录下创建一个代表相应软件的shell脚本,通过该脚本来设置环境变量。但是需要注意,不能在/etc/environment 中使用表达式

Note: Variable expansion does not work in /etc/environment.

---------------------------------分隔线--------------------------------

二,比如,当你在~/.bashrc 中设置环境变量时,下面这段话解释了怎样来设置环境变量。

使用 赋值符号(=)只是创建了一个shell 变量,当使用export 之后,该shell变量变成环境变量。

If we use an assignment expression for a variable that doesn't exist, the shell will create a
shell variable, which is similar to an environment variable but does not influence the behaviour of other applications.

A shell variable can be exported to become an environment variable with the
export command.

三,关于/etc/bash.bashrc 中设置环境变量:

在这里设置的环境变量在非登陆shell中是不起作用的。默认情况下,在图形界面中启动的shell 会读取不到 这里面的设置值。

The shell config file /etc/bash.bashrc is sometimes suggested for setting environment variables system-wide. While this may work on Bash shells for programs started from the shell, variables set in that file are not available by default to
programs started from the graphical environment in a desktop session.

四,环境变量中的继承关系:

当你在图形界面打开一个shell 后,这个bash 是父进程,在该shell下运行 vim,则vim会继承该bash的所有环境变量。当我们在该shell下运行vim之前,如:设置了一个新的值给LANG,然后再运行vim,则vim会使用新指定的LANG来显示语言。

五,当你修改了环境变量并运行source使之生效时,一定要注意:对于其它已经打开了的终端而言,它们并不会受到影响。因为它们读入的设置是source之前的设置。所以一定要把已经打开的terminal关闭了再打开,这样才能看出source是否已经生效。

change a value of an environment variable in one of the windows, that change will not be seen by the shell in the other window or any other program currently on the desktop.



综上所述,当需要添加环境变量时,首先要考虑是全局的还是用户的。若是全局的,在/etc/profile.d/ 下新建一个*.sh来添加。若是局部的,在~/.bashrc 下加入环境变量。


Ubuntu14.04 为什么 在/etc/profile.d/目录下添加的环境变量后,在非登陆SHELL中(图形界面)打开一个终端(ctrl+alt+t)时,添加的环境变量并不起作用?而在另一台电脑上使用SSH远程登陆到该机子时,却可以在非登陆SHELL中看到新添加的环境变量????

上述情况确实存在,要想在图形界面终端(ctrl + alt + t)使用命令,则只有在 ~/.bashrc 下面再去配置所安装软件的路径,这样就可以在图形界面终端使用命令来操作了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: