I am running RHEL6, 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?
I am running RHEL6, 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?
You can add it to your shell configuration file, e.g. $HOME/.bashrc
or more globally in /etc/environment
.
You have to edit three files to set a permanent environment variable as follow:
Normally you have to restart your computer to apply this 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 )
#!/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 executable and just run it!
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.
On Ubuntu systems, use the following locations:
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
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.
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:
j6.sh
script which simply does export JAVA_HOME=...
path to j6 install...j6.sh
in that respective command terminal. By default, my JAVA_HOME
is set to J7.Hope this helps.