Setting environment variables permanently under GNU-Linux systems

8,542

Solution 1

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.

Solution 2

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:
8,542

Related videos on Youtube

NeDark
Author by

NeDark

Updated on September 18, 2022

Comments

  • NeDark
    NeDark over 1 year

    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

  • Keith
    Keith over 12 years
    Note that the last one only works in bash. Older shells don't allow it.