220

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

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1340582
  • 19,151
  • 35
  • 115
  • 171

6 Answers6

248

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 systems. You have to exit the terminal or create a new one and on the server, log out the session and log in to reflect these changes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Antoine
  • 13,494
  • 6
  • 40
  • 52
  • 3
    Thanks, that's helpful. How can I make changes take effect without restart ? *(I'm Linux newbie)* – Bitterblue Nov 28 '13 at 08:13
  • 15
    @mini-me - ~/bashrc is pulled each time you open a shell. To load it explicitly, use `source` e.g. - `> source ~/.bashrc`. – kostja Nov 28 '13 at 08:20
  • @kostja For all **new** shells ? If I have eclipse running before I change those env variables and I want eclipse to have changed vars, do I have to restart eclipse ? – Bitterblue Nov 28 '13 at 09:39
  • 1
    @mini-me: the environment of a process is usually set by the caller and changed from within the process. Changing env from outside a running process is unusual and not doable with `export`, but [try with a debugger](http://stackoverflow.com/questions/205064/is-there-a-way-to-change-another-processs-environment-variables) – Antoine Nov 28 '13 at 09:44
  • @mini-me - it depends on the file. As I said before, ** ~/.bashrc is pulled automatically each time you open a shell** - so you dont have to `source` the `.bashrc` explicitly, except you want to pull changes into an already open shell. You will have to `source` changes in **all** other configuration files on each change for **every** shell if you dont want to restart. – kostja Nov 28 '13 at 09:44
  • Where should I put that line?! bottom of page? – Dr.jacky Dec 06 '15 at 05:38
  • 2
    @Mr.Hyde: It usually doesn't matter. Files are parsed from top to bottom, so if a var definition depends on another, they should be ordered accordingly. So yes the end of the file is fine. – Antoine Dec 09 '15 at 09:58
  • 1
    just note `Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.` from https://help.ubuntu.com/community/EnvironmentVariables – Toolkit Sep 27 '17 at 05:05
183

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 bashrc you have to put the mentioned command line at the end of this file to have your environment variable in every login of your OS.

  • /etc/environment

    If you want your environment variable in every window 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 these changes. But you can apply changes in bashrc and profile by these commands:

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

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

A Simple Solution

I've written a simple script for these procedures to do all those work. You just have to set the 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 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 these lines in a shfile then make it executable and just run it!

Dharman
  • 30,962
  • 25
  • 85
  • 135
e3oroush
  • 3,045
  • 1
  • 17
  • 26
  • 1
    The script worked very well. I have Ubuntu 14.04 installed. I just pasted the content above in a blank file, named it **insert_var.sh**, then `chmod -x insert_var.sh`, then `bash insert_var.sh`. Thanks. – Brunno Vodola Martins Apr 11 '16 at 17:46
  • I opened a Terminal Window in Ubuntu and enter export MY_VAR=1. Where will this Variable MY_VAR stored ? – vgokul129 Nov 15 '19 at 06:52
  • Your variable `MY_VAR` will only be part of the current shell that you are working on. If you close the shell, the variable will be inaccessible. – Muhammad Tariq Jan 24 '21 at 11:18
  • 1
    I tested the approach with just the `/etc/environment` file on CentOS 8 and I didn't need to restart I just needed to exit (log out) the terminal or create a new one (log in). – Eduardo Lucio Aug 04 '21 at 01:58
33

On Ubuntu systems, use the following locations:

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

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

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

    ~/.pam_environment
    

For more details on #2, check this Ask Ubuntu answer.

NOTE: #3 is the Ubuntu recommendation, but it may have security concerns in the real world.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
csi
  • 9,018
  • 8
  • 61
  • 81
32

Add the line to your .bashrc file or .profile.

The variables set in file $HOME/.profile are active for the current user, and the ones in /etc/profile are global. The .bashrc file is pulled on each Bash session start.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kostja
  • 60,521
  • 48
  • 179
  • 224
7

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

Source: How to Set Environment Variables in Linux

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dasilvadaniel
  • 413
  • 4
  • 8
3

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:

  • created j6.sh script which simply does export JAVA_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, my JAVA_HOME is set to J7.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cristian Botiza
  • 419
  • 4
  • 9