In order to set environment variables in a way that affects a particular user's environment, one should not place commands to set their values in particular shell script files in the user's home directory, but use:
~/.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.
Not recommended:
~/.profile - This is probably the best file for placing environment variable assignments in, since it gets executed automatically by the DisplayManager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.
--We are going to edit "etc\profile". The environment variables are to be input at the bottom of the file. Since Ubuntu does not give access to root folder, we will have to use a few commands in the terminal
Step1: Start Terminal. Type in command: gksudo gedit /etc/profile
Step2: The profile text file will open. Enter the environment variables at the bottom of the page........... Eg:export JAVA_HOME=/home/alex/jdk1.6.0_22/bin/java
export PATH=/home/alex/jdk1.6.0_22/bin:$PATH
step3: save and close the file. Check if the environment variables are set by using echo command........ Egecho $PATH
You need to put variable definition in the ~/.bashrc file.
From bash man page:
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.
Traditionally, if you only want to change the variable in your terminal windows, set it in .bashrc file, which is sourced each time a new terminal is opened. .profile file is not sourced each time you open a new terminal.
.bashrc should solve your problem. However, it is not the proper solution since you are using Ubuntu. See the relevant Ubuntu help page "Session-wide environment variables". Thus, no wonder that .profile does not work for you. I use Ubuntu 12.04 and xfce. I set up my .profile and it is simply not taking effect even if I log out and in. Similar experience here. So you may have to use .pam_environment file and totally forget about .profile, and .bashrc. And NOTE that .pam_environment is not a script file.
I know this is a long cold question, but it comes up every time there is a new or recent major Java release. Now this would easily apply to 6 and 7 swapping.
发布评论
评论(8)
将环境变量放入全局
/etc/environment
文件中:在要更新变量的每个 shell 中执行“source /etc/environment”:
检查它是否有效:
很好,无需注销。
如果您只想在终端中设置 JAVA_HOME 环境变量,请在 ~/.bashrc 文件中设置它。
Put the environment variables into the global
/etc/environment
file:Execute "source /etc/environment" in every shell where you want the variables to be updated:
Check that it works:
Great, no logout needed.
If you want to set JAVA_HOME environment variable in only the terminal, set it in ~/.bashrc file.
这可能会解决您的问题:
https://help.ubuntu.com/community/EnvironmentVariables
This will probably solve your problem:
https://help.ubuntu.com/community/EnvironmentVariables
尝试这些步骤。
--我们要编辑“etc\profile”。
环境变量将在文件底部输入。由于 Ubuntu 没有
授予对根文件夹的访问权限,我们必须在终端中使用一些命令
第 1 步:启动终端。输入命令:
gksudo gedit /etc/profile
第 2 步:配置文件文本文件将打开。在页面底部输入环境变量............ 例如:
export JAVA_HOME=/home/alex/jdk1.6.0_22/bin/java< /code>
步骤3:保存并关闭文件。使用echo命令检查环境变量是否设置...... Eg
echo $PATH
Try these steps.
--We are going to edit "etc\profile".
The environment variables are to be input at the bottom of the file. Since Ubuntu does not
give access to root folder, we will have to use a few commands in the terminal
Step1: Start Terminal. Type in command:
gksudo gedit /etc/profile
Step2: The profile text file will open. Enter the environment variables at the bottom of the page........... Eg:
export JAVA_HOME=/home/alex/jdk1.6.0_22/bin/java
step3: save and close the file. Check if the environment variables are set by using echo command........ Eg
echo $PATH
您需要将变量定义放在
~/.bashrc
文件中。来自 bash 手册页:
You need to put variable definition in the
~/.bashrc
file.From bash man page:
传统上,如果您只想更改终端窗口中的变量,请将其设置在
.bashrc
文件中,每次打开新终端时都会获取该文件。每次打开新终端时,不会获取.profile
文件。查看相关 .profile 和 .bashrc 之间的区别:
.bashrc、.bash_profile 和 .environment 之间有什么区别?
.bashrc
应该可以解决您的问题。然而,这不是正确的解决方案,因为您使用的是 Ubuntu。请参阅相关的 Ubuntu 帮助页面“会话范围的环境变量”。因此,难怪.profile
不适合您。我使用 Ubuntu 12.04 和 xfce。我设置了我的.profile
,即使我注销并登录,它也根本没有生效。类似的体验 此处。因此,您可能必须使用.pam_environment
文件,并完全忘记.profile
和.bashrc
。请注意,.pam_environment
不是脚本文件。Traditionally, if you only want to change the variable in your terminal windows, set it in
.bashrc
file, which is sourced each time a new terminal is opened..profile
file is not sourced each time you open a new terminal.See the difference between .profile and .bashrc in question:
What's the difference between .bashrc, .bash_profile, and .environment?
.bashrc
should solve your problem. However, it is not the proper solution since you are using Ubuntu. See the relevant Ubuntu help page "Session-wide environment variables". Thus, no wonder that.profile
does not work for you. I use Ubuntu 12.04 and xfce. I set up my.profile
and it is simply not taking effect even if I log out and in. Similar experience here. So you may have to use.pam_environment
file and totally forget about.profile
, and.bashrc
. And NOTE that.pam_environment
is not a script file.看一下
bash(1)
,你需要登录 shell 用于获取~/.profile
,即-l
选项。Take a look at
bash(1)
, you need a login shell to pickup the~/.profile
, i.e. the-l
option.我知道这是一个很长的冷问题,但每次有新的或最近的主要 Java 版本发布时都会出现这个问题。现在这很容易适用于 6 和 7 的交换。
我过去曾使用 update-java-alternatives 完成此操作:
http://manpages.ubuntu.com/manpages/hardy /man8/update-java-alternatives.8.html
I know this is a long cold question, but it comes up every time there is a new or recent major Java release. Now this would easily apply to 6 and 7 swapping.
I have done this in the past with
update-java-alternatives
:http://manpages.ubuntu.com/manpages/hardy/man8/update-java-alternatives.8.html
对 .profile 进行更改后,您需要执行该文件以使更改生效。
完成此操作后,echo 命令将起作用。
After making changes to .profile, you need to execute the file, in order for the changes to take effect.
Once this is done, the echo command will work.