如何在Linux中永久导出变量?

时间:2021-08-17 11:41:45

I am running RHEL6, and I have exported an environment variable like this:

我正在运行RHEL6,并导出了如下环境变量:

export DISPLAY=:0

That variable is lost when the CMD is closed. How do I permanently add this so that this variable value always exists with a particular user?

当CMD关闭时,该变量将丢失。如何永久地添加它,使这个变量值始终存在于特定的用户中?

5 个解决方案

#1


118  

You can add it to your shell configuration file, e.g. $HOME/.bashrc or more globally in /etc/environment.

您可以将它添加到shell配置文件中,例如$HOME/。bashrc或更多全局/etc/environment中。

#2


58  

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
  • ~ /。当您打开任何终端窗口时,此文件将被运行。因此,如果您希望在所有终端窗口中都有一个永久环境变量,您必须在这个文件的末尾添加以下一行:export DISPLAY=0

  • ~/.profile

  • ~ / . profile
  • Same as bashrc you have to put the mentioned command line at the end of this file to have your environment variable in the every log in of your OS.

  • /etc/environment

  • /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 export command

Normally you have to restart your computer to apply this changes. But you can apply changes in bashrc and profile by these commands:

通常,您必须重新启动计算机才能应用这些更改。但是您可以通过以下命令在bashrc和profile中应用更改:

$ source ~/.bashrc
$ source ~/.profile

源~ /美元。bashrc美元来源(~ / . profile

But for /etc/environemnt you have no choice but restarting ( as far as I know )

但是对于/ etc/environment你没有选择,只能重新开始(据我所知)

  • A Simple Solution

  • 一个简单的解决方案
  • I've written a simple script for this procedures to do all those work. You just have to set name and value of your environment variable.

    #!/bin/bash
    echo "Enter variable name: "
    read variable_name
    echo "Enter variable value: "
    read variable_value
    echo "adding " $variable_name " to einvironment 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 wanna 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 executable and just run it!

    将这些行保存在一个shfile中,然后让它可执行,然后运行它!

    #3


    27  

    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/。配置文件对当前用户是活动的,/etc/profile中的配置文件是全局的。在每次bash会话启动时,都会拉出.bashrc。

    #4


    23  

    On Ubuntu systems, use the following locations:

    在Ubuntu系统上,使用以下位置:

    1. System-wide persistent variables in the format of JAVA_PATH=/usr/local/java store in

      系统范围内的持久化变量,格式为JAVA_PATH=/usr/local/java

      /etc/environment
      
    2. System-wide persistent variables that reference variables such as
      export PATH="$JAVA_PATH:$PATH" store in

      引用诸如export PATH=“$JAVA_PATH:$PATH”之类变量的系统范围的持久变量

      /etc/.bashrc
      
    3. User specific persistent variables in the format of PATH DEFAULT=/usr/bin:usr/local/bin store in

      用户特定的持久变量格式为路径默认=/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的更多细节,请检查这个问Ubuntu的答案。注意:#3是Ubuntu推荐,但在现实世界中可能存在安全问题。

    #5


    1  

    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 export JAVA_HOME=... path to j6 install...
    • 创建的卫星。sh脚本,它只导出JAVA_HOME=…卫星路径安装…
    • 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, my JAVA_HOME is set to J7.
    • 然后,正如上面的一条评论所建议的,每当我需要J6进行构建时,我都会运行源代码J6。在相应的命令终端中的sh。默认情况下,我的JAVA_HOME设置为J7。

    Hope this helps.

    希望这个有帮助。

    #1


    118  

    You can add it to your shell configuration file, e.g. $HOME/.bashrc or more globally in /etc/environment.

    您可以将它添加到shell配置文件中,例如$HOME/。bashrc或更多全局/etc/environment中。

    #2


    58  

    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
    • ~ /。当您打开任何终端窗口时,此文件将被运行。因此,如果您希望在所有终端窗口中都有一个永久环境变量,您必须在这个文件的末尾添加以下一行:export DISPLAY=0

    • ~/.profile

    • ~ / . profile
    • Same as bashrc you have to put the mentioned command line at the end of this file to have your environment variable in the every log in of your OS.

    • /etc/environment

    • /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 export command

    Normally you have to restart your computer to apply this changes. But you can apply changes in bashrc and profile by these commands:

    通常,您必须重新启动计算机才能应用这些更改。但是您可以通过以下命令在bashrc和profile中应用更改:

    $ source ~/.bashrc
    $ source ~/.profile

    源~ /美元。bashrc美元来源(~ / . profile

    But for /etc/environemnt you have no choice but restarting ( as far as I know )

    但是对于/ etc/environment你没有选择,只能重新开始(据我所知)

  • A Simple Solution

  • 一个简单的解决方案
  • I've written a simple script for this procedures to do all those work. You just have to set name and value of your environment variable.

    #!/bin/bash
    echo "Enter variable name: "
    read variable_name
    echo "Enter variable value: "
    read variable_value
    echo "adding " $variable_name " to einvironment 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 wanna 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 executable and just run it!

    将这些行保存在一个shfile中,然后让它可执行,然后运行它!

    #3


    27  

    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/。配置文件对当前用户是活动的,/etc/profile中的配置文件是全局的。在每次bash会话启动时,都会拉出.bashrc。

    #4


    23  

    On Ubuntu systems, use the following locations:

    在Ubuntu系统上,使用以下位置:

    1. System-wide persistent variables in the format of JAVA_PATH=/usr/local/java store in

      系统范围内的持久化变量,格式为JAVA_PATH=/usr/local/java

      /etc/environment
      
    2. System-wide persistent variables that reference variables such as
      export PATH="$JAVA_PATH:$PATH" store in

      引用诸如export PATH=“$JAVA_PATH:$PATH”之类变量的系统范围的持久变量

      /etc/.bashrc
      
    3. User specific persistent variables in the format of PATH DEFAULT=/usr/bin:usr/local/bin store in

      用户特定的持久变量格式为路径默认=/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的更多细节,请检查这个问Ubuntu的答案。注意:#3是Ubuntu推荐,但在现实世界中可能存在安全问题。

    #5


    1  

    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 export JAVA_HOME=... path to j6 install...
    • 创建的卫星。sh脚本,它只导出JAVA_HOME=…卫星路径安装…
    • 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, my JAVA_HOME is set to J7.
    • 然后,正如上面的一条评论所建议的,每当我需要J6进行构建时,我都会运行源代码J6。在相应的命令终端中的sh。默认情况下,我的JAVA_HOME设置为J7。

    Hope this helps.

    希望这个有帮助。