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

参考 :  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 下面再去配置所安装软件的路径,这样就可以在图形界面终端使用命令来操作了。
Ubuntu系统设置环境变量通常有几种方法,可以根据需要选择适合的方式来进行环境变量设置。 1. 当前会话临时设置: 打开终端,可以直接使用export命令设置环境变量,例如: ``` export PATH=$PATH:/new/path ``` 这里的`PATH`是环境变量的名称,`/new/path`是要添加到环境变量的新路径。这种方式设置环境变量只对当前会话有效。 2. 修改用户的shell配置文件: 对于bash shell,通常需要修改或添加以下配置文件: - `.bashrc`:当前用户登录时和打开新终端时都会读取。 - `.bash_profile`:当前用户登录时读取。 打开这些文件,通常位于用户的主目录下,然后在文件添加export命令,例如: ``` export PATH=$PATH:/new/path ``` 修改后,需要使用source命令或重新登录使更改生效: ``` source ~/.bashrc ``` 3. 系统级别永久设置: 为了设置对所有用户都生效的环境变量,可以编辑`/etc/environment`文件: ``` sudo gedit /etc/environment ``` 在打开的文件,添加或修改环境变量的值,例如: ``` PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/new/path" ``` 保存后,需要重启计算机使更改生效。 4. 使用`/etc/profile`或`/etc/profile.d/`目录脚本: 对于系统级别对所有用户生效的设置,可以编辑`/etc/profile`文件或在`/etc/profile.d/`目录下添加一个新的脚本文件,并在文件添加export命令。 设置环境变量时需要注意的是,环境变量的名称和值之间用等号`=`连接,并且等号两边不要有空格。如果要添加新的路径到PATH环境变量,需要在冒号`:`分隔的路径列表末尾添加新路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值