2

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

1

2 Answers 2

6

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.

3

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

1
  • Note that the last one only works in bash. Older shells don't allow it.
    – Keith
    Aug 26, 2011 at 2:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .