Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to add a directory to search my search path. I know I have to modify the $PATH environment variable. However, I want the change to be permanent, so that it is always in effect, for every Terminal (bash) window I open.

There is an overload of confusing and possibly conflicting information on https://help.ubuntu.com/community/EnvironmentVariables

I am using Ubuntu 10.04. Suppose I want to add /usr/local/foo to my $PATH. Which file (.bashrc, .profile, .bash_login, etc...) should I modify and what should the new line(s) look like?

share|improve this question
add comment

3 Answers

up vote 21 down vote accepted

The following command adds a path to your current path:

export PATH=$PATH:/my/custom/path

If you want your setup to execute this command every time, there are a number of places where you can put it. When you login, the following scripts will be executed in this order:

/etc/profile      (which starts by loading everything in /etc/profile.d)
~/.profile        (which starts by loading ~/.bashrc if you are running bash)

Notes

  • ~/.profile is only loaded if ~/.bash_profile or ~/.bash_login DO NOT EXIST. Otherwise, at least bash, will load them instead. It is suggest to use .profile and not the bash specific scripts. So, if in these attempts you created .bash_login, please delete it now.

  • ~/.bashrc is only loaded if you are running an interactive session. (something with a prompt where you can actually type something.

  • ~/.bashrc is loaded again and again, everytime you open up a new terminal. So a new tab in gnome-terminal, a new virtual terminal, etc. So even if you don't login again, .bashrc is loaded (and thereby resets its environment) everytime you open a new shell.

  • Something like byobu should really go into .profile, (otherwise it won't work ;-)

  • Something like paths should go into .profile if you want it to work outside of the interactive sessions. (say when you press alt+f2 in gnome)

share|improve this answer
    
I'll mark this as the answer if you update it to include the requested export line that should be added to .profile. –  Joshua Flanagan Sep 8 '10 at 0:13
    
This used to be valid only for console logins (e.g. ssh, or the virtual terminals accessible for Ctrl+Alt+Fx). I didn't know that /etc/gdm/Xsession sources ~/.profile these days. Neat! –  Marius Gedminas Sep 9 '10 at 14:31
    
Yeah, i didn't mention /etc/gdm/Xsession specifically or ~/.Xprofile because there are better ways to make graphical programs launch at start up, which does garantuee that the rest of the environment is already loaded. –  Ralf Sep 10 '10 at 15:14
    
to make this answer more comprehensive please add MattH's comment about sourcing ~/.profile to activate changes without a logoff/on cycle. –  matt wilkie Nov 17 '10 at 21:36
    
I am running 10.10 and I made sure ~/.bash_profile and ~/bash_login don't exist but my ~/.profile still doesn't seem to be executed when I open the terminal I tested by adding echo hello to the bottom of it and its not showing up. –  schwiz Feb 20 '11 at 7:47
show 2 more comments

I got it to work by modifying ~/.profile

It looks like adding ~/bin to my path was a bad example, as there is already code in ~/.profile to do that automatically, if the directory exists.

To add the usr/local/foo directory to my path for every session going forward, I add/edit the following line at the end of my .profile:

export PATH=$PATH:/usr/local/foo

However, to make this take effect, I needed to log out and log back in (simply closing the Terminal window and opening a new one did NOT work).

share|improve this answer
4  
Make that export PATH="$PATH:/usr/foo", in case you ever have spaces or other special characters in $PATH. –  Gilles Sep 6 '10 at 19:52
2  
You can reload the current environment without logging out by typing ". ~/.profile" –  Matt H Sep 7 '10 at 2:30
    
@MattH: no you can't. if you source ~/.profile in a given terminal, it will be effective for that terminal only –  MestreLion Apr 11 '13 at 8:32
    
@MestreLion - you are right. I was mentioning it for convenience for the current terminal. Forgot to add that. –  Matt H Apr 18 '13 at 1:18
add comment

I think adding the path to /etc/environment will help..

share|improve this answer
add comment

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.