Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm running Ubuntu 11.04. I use the terminal to start a bash session, and I want to add an environment variable:

$r@hajt:~$ env THEVAR=/example

But it's not working. It shows all the variables with THEVAR being the last one, but another call to env does not show THEVAR anymore- env | grep THEVAR returns nothing.

Similarly, scripts with export (export THEVAR=/example) or other variable assignments (THEVAR=/example) don't add the environment variable.

I know I'm doing something wrong, I know it should be something simple, but I just can't find what.

UPDATE: The real meaning of my question was this one:

(Anyway I'll choose the most voted answer and leave the edited title -that wasn't what I was asking)

env runs a program in a modified environment, then dismisses all the changes.

share|improve this question
When defined/exported in .bashrc, it works properly. – huff Aug 27 '11 at 0:26
possible duplicate of Google Earth and $BROWSER environment variable – Alvin Row Aug 27 '11 at 3:09
How about THEVAR=/example? – Oleh Prypin Aug 27 '11 at 6:34
The other is about global variables, this one does not add such a limitation. I'd keep it separate. – Lekensteyn Aug 27 '11 at 8:05
up vote 64 down vote accepted

To set variable only for current shell:

VARNAME="my value"

To set it for current shell and all processes started from current shell:

export VARNAME="my value"      # shorter, less portable version

To set it permanently for all future bash sessions add such line to your .bashrc file in your $HOME directory.

To set it permanently, and system wide (all users, all processes) add set variable in /etc/environment:

gksudo gedit /etc/environment
share|improve this answer
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. – Sudo SU Root Aug 22 '15 at 8:38
Where should I add my specific line?! bottom of .bashrc page?! – Mr.Hyde Dec 6 '15 at 5:41
@Mr.Hyde yes, that's probably a reasonable place – Michał Šrajer Dec 7 '15 at 16:55
Why is the second one less portable? – Bharadwaj Raju Mar 17 at 1:22
@BharadwajRaju because on some old UNIX systems one can export only variable that is set already. Old Solaris and HP-UX for example. – Michał Šrajer Mar 18 at 17:19

To set an environment variable once, use the export command in the prompt, not in a shell script:

$ export THEVAR=/example

The variable will be set for the rest of the shell session or until unset.

To set an environment variable everytime, use the export command in the .bashrc file (or the appropriate initialization file for your shell).

To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work.

For an explanation of the difference between sourcing and executing see this answer:

share|improve this answer

To permanently add a new environment variable in Ubuntu (tested only in 14.04), use the following steps:

  1. Open a terminal (by pressing CtrlAltT)
  2. sudo -H gedit /etc/environment
  3. Type your password
  4. Edit the text file just opened:
    e.g. if you want to add FOO=bar, then just write FOO=bar in a new line
  5. Save it
  6. Once saved, logout and login again.
  7. Your required changes are made.
share|improve this answer
I have cleaned up your answer removing the more dangerous bits about sudo gedit (See…) and chmod 777. The latter should never be done on a system configuration file. – muru Dec 8 '14 at 13:45

Your Answer


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.