Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I was using the command export, but it looks that after some time the set variables disappears. What is the easiest way of setting an environment variable forever?

Thanks

share|improve this question
2  
See also: How do I set PATH and other environment variables? –  slhck Aug 25 '11 at 21:52

2 Answers 2

up vote 3 down vote accepted

The export variables in the .profile or .bash_profile are available when you start a terminal only and not visible to applications started outside the terminal. Use the /etc/environment to make the variables available to all the applications outside the terminal.

share|improve this answer

According to the version of Linux, and assuming the standard bash shell is used, the user in question will have a .profile or .bash_profile file in their home folder (it's the latter in Ubuntu). You can use your favourite editor to add the variable definition there - for example:

Before:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

After:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
MYVARIABLE=THISVALUE

export PATH MYVARIABLE

The export command pushes the variable out into the child environment so it's available to other scripts and processes. You don't have to pile up the variable names behind a single export command, you can also do the whole definition and export in one go eg:

export MYVARIABLE=THISVALUE

Global variables for everyone can be set in a similar way in /etc/profile

share|improve this answer
    
Note that the last one only works in bash. Older shells don't allow it. –  Keith Aug 26 '11 at 2:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.