如何在 Linux 中永久导出变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13046624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to permanently export a variable in Linux?
提问by user1340582
I am running RHEL6, and I have exported an environment variable like this:
我正在运行 RHEL6,并且我已经导出了一个这样的环境变量:
export DISPLAY=:0
That variable is lost when the terminal is closed. How do I permanently add this so that this variable value always exists with a particular user?
当终端关闭时,该变量将丢失。如何永久添加它,以便此变量值始终存在于特定用户中?
采纳答案by Antoine
You can add it to your shell configuration file, e.g. $HOME/.bashrc
or more globally in /etc/environment
.
After adding these lines the changes won't reflect instantly in GUI based system's you have to exit the terminal or create a new one and in server logout the session and login to reflect these changes.
您可以将它添加到您的 shell 配置文件中,例如$HOME/.bashrc
或在/etc/environment
. 添加这些行后,更改不会立即反映在基于 GUI 的系统中,您必须退出终端或创建一个新终端,然后在服务器中注销会话并登录以反映这些更改。
回答by kostja
add the line to your .bashrc
or .profile
. The variables set in $HOME/.profile
are active for the current user, the ones in /etc/profile
are global. The .bashrc
is pulled on each bash session start.
将该行添加到您的.bashrc
或.profile
. 中设置的变量$HOME/.profile
对当前用户有效,中的变量/etc/profile
是全局的。该.bashrc
拉各的bash会话开始。
回答by csi
On Ubuntu systems, use the following locations:
在 Ubuntu 系统上,使用以下位置:
System-wide persistent variables in the format of
JAVA_PATH=/usr/local/java
store in/etc/environment
System-wide persistent variables that reference variables such as
export PATH="$JAVA_PATH:$PATH"
store in/etc/.bashrc
User specific persistent variables in the format of
PATH DEFAULT=/usr/bin:usr/local/bin
store in~/.pam_environment
JAVA_PATH=/usr/local/java
store in格式的系统范围的持久变量/etc/environment
引用变量的系统范围的持久变量,例如
export PATH="$JAVA_PATH:$PATH"
store in/etc/.bashrc
PATH DEFAULT=/usr/bin:usr/local/bin
存储在格式中的用户特定的持久变量~/.pam_environment
For more details on #2, check this Ask Ubuntu answer. NOTE: #3 is the Ubuntu recommendation but may have security concerns in the real world.
有关#2 的更多详细信息,请查看此 Ask Ubuntu answer。注意:#3 是 Ubuntu 的建议,但在现实世界中可能存在安全问题。
回答by Cristian Botiza
A particular example:
I have Java 7 and Java 6 installed, I need to run some builds with 6, others with 7. Therefore I need to dynamically alter JAVA_HOME
so that maven picks up what I want for each build. I did the following:
一个特定的例子:我安装了 Java 7 和 Java 6,我需要用 6 运行一些构建,其他用 7 运行。因此我需要动态更改,JAVA_HOME
以便 maven 为每个构建选择我想要的。我做了以下事情:
- created
j6.sh
script which simply does exportJAVA_HOME=...
path to j6 install... - then, as suggested by one of the comments above, whenever I need J6 for a build, I run source
j6.sh
in that respective command terminal. By default, myJAVA_HOME
is set to J7.
- 创建的
j6.sh
脚本只是将JAVA_HOME=...
路径导出到 j6 安装... - 然后,正如上述评论之一所建议的那样,每当我需要 J6 进行构建时,我都会
j6.sh
在相应的命令终端中运行源代码。默认情况下,myJAVA_HOME
设置为 J7。
Hope this helps.
希望这可以帮助。
回答by e_soroush
You have to edit three files to set a permanent environment variable as follow:
您必须编辑三个文件以设置永久环境变量,如下所示:
~/.bashrc
When you open any terminal window this file will be run. Therefore, if you wish to have a permanent environment variable in all of your terminal windows you have to add the following line at the end of this file:
export DISPLAY=0~/.profile
Same as bashrcyou have to put the mentioned command line at the end of this file to have your environment variable in the every login of your OS.
/etc/environment
If you want your environment variable in every windows or application ( not just terminal window ) you have to edit this file. Add the following command at the end of this file:
DISPLAY=0
Note that in this file you do not have to write exportcommand
~/.bashrc
当您打开任何终端窗口时,将运行该文件。因此,如果您希望在所有终端窗口中都有一个永久的环境变量,您必须在此文件的末尾添加以下行:
export DISPLAY=0~/.profile
与bashrc一样,您必须将提到的命令行放在此文件的末尾,以便在每次登录操作系统时都使用环境变量。
/etc/环境
如果您希望在每个窗口或应用程序(不仅仅是终端窗口)中使用环境变量,您必须编辑此文件。在此文件的末尾添加以下命令:
DISPLAY=0
请注意,在此文件中您不必编写export命令
Normally you have to restart your computer to apply this changes. But you can apply changes in bashrcand profileby these commands:
通常,您必须重新启动计算机才能应用此更改。但是您可以通过以下命令在bashrc和profile 中应用更改:
$ source ~/.bashrc
$ source ~/.profile
$ source ~/.bashrc
$ source ~/.profile
But for /etc/environmentyou have no choice but restarting ( as far as I know )
但是对于/etc/environment你别无选择,只能重新启动(据我所知)
A Simple Solution
一个简单的解决方案
#!/bin/bash
echo "Enter variable name: "
read variable_name
echo "Enter variable value: "
read variable_value
echo "adding " $variable_name " to environment variables: " $variable_value
echo "export "$variable_name"="$variable_value>>~/.bashrc
echo $variable_name"="$variable_value>>~/.profile
echo $variable_name"="$variable_value>>/etc/environment
source ~/.bashrc
source ~/.profile
echo "do you want to restart your computer to apply changes in /etc/environment file? yes(y)no(n)"
read restart
case $restart in
y) sudo shutdown -r 0;;
n) echo "don't forget to restart your computer manually";;
esac
exit
Save this lines in a shfile then make it executableand just run it!
将此行保存在 shfile 中,然后使其可执行并运行它!
回答by dasilvadaniel
If it suits anyone, here are some brief guidelines for adding environment variables permanently.
如果它适合任何人,这里有一些关于永久添加环境变量的简要指南。
vi ~/.bash_profile
Add the variables to the file:
将变量添加到文件中:
export DISPLAY=:0
export JAVA_HOME=~/opt/openjdk11
Immediately apply all changes:
立即应用所有更改:
source ~/.bash_profile
来源:https: //www.serverlab.ca/tutorials/linux/administration-linux/how-to-set-environment-variables-in-linux/