Do I put PATH in bash_profile or bashrc or both?
Ok I know that the .bash_profile is read only when I login. And the .bashrc file is read for every other shell (ie. a terminal) after I initially login.
Does this mean If I want to add a directory to my PATH variable I need to add it to the .bash_profile to be read during my initial login. And also add it to the PATH variable in my .bashrc file to be read for every other subsequent subshell opened. Or is there a simpler way. Thank you. |
I have it in my .bash_profile, not in .bashrc - I've never had any problems with it changing when I open non-login shells afterwards...
|
My .bash_profile will automatically add a ~/bin directory to the PATH variable if it's been created which I have. Yet if I'm on a terminal for instance and I open one or two more shells in that terminal the ~/bin path is not there. I'm currently using Ubuntu Edgy. Here is a copy of my .bash_profile file:
newbie@newbie-desktop:~$ more .bash_profile # ~/.bash_profile: executed by bash(1) for login shells. # see /usr/share/doc/bash/examples/startup-files for examples. # the files are located in the bash-doc package. # the default umask is set in /etc/login.defs umask 077 # include .bashrc if it exists if [ -f ~/.bashrc ]; then . ~/.bashrc fi # set PATH so it includes user's private bin if it exists if [ -d ~/bin ] ; then PATH=~/bin:"${PATH}" fi export PATH Now If I add the ~/bin path to the PATH variable in the .bashrc file export it then source the file ~/bin path is in every subshell. Any ideas. I noticed that while the ~/bin path is in the .bashrc file if I go to a console ~/bin is at the beginning and the end when I issue the echo $PATH command. I guess two files are getting sourced? Any input? |
I'm not sure how Ubuntu invokes the shell for you, but I'm assuming the following because it's what I'm using here:
- Your shell in /etc/passwd is /bin/bash; - The virtual consoles (accessed with Alt-F1, Alt-F2, etc. outside of the GUI and Ctrl-Alt-F1, Ctrl-Alt-F2, etc. inside the GUI) are login shells; - Your terminal windows open as login shells (when you select xterm from the menu); - Running /bin/bash from the command line in a terminal window starts a non-login shell. If that's the case, try adding the line echo Finished .bash_profile to the end of .bash_profile and echo Finished .bashrc to the end of .bashrc. Now, when you open access different types of consoles/terminals you should get the echo statements printed to your screen. On my system, for login shells .bash_profile is accessed and it in turn calls .bashrc. On non-login shells only .bashrc is accessed. Can you try it with the echo commands so we can see what types of sessions are being started. That should provide more info for diagnosing what is going on. |
Thanks gilead.
When I login via console I get: "Finished .bashrc." "Finished .bash_profile." When I open a terminal via GUI I get: "Finished .bashrc." I'm going to work now. I will be on later today. Thanks. So if the .bashrc file is read first when I login and other subsequent subshells should I just put the PATH and other things (like umask etc.) in the .bashrc file? Just curious. |
The .bashrc file is sourced first because it's referenced at the start of your .bash_profile. I'm not sure why you're getting those results with your path since your shell looks to be behaving the same way as mine.
To get around this you can put your path statements in your .bashrc (although it's not usual), but check that you're not getting an ever expanding path variable. For example, if your have export PATH=/some/path:$PATH in your .bashrc and you run several nested interactive shells, you'll have the path ending up as /some/path:/some/path:/some/path:/some/path:original/path or worse. One last thought, in your .bashrc, does it preserve your original path, or is the statement something like export PATH=~/bin? |
I thought that if I exported the PATH variable in the .bash_profile it should be available to all subshells including any number of terminal sessions I have going on. Any way if I have ~/bin in my .bash_profile it's read when I login via console and if I start a terminal shell. But in any subsequent new terminal shells ~/bin is not in the PATH.
If I put the command "export PATH=$PATH:~/bin" in my .bashrc file and comment out any PATH references in the .bash_profile ~/bin is read when I login via console, when I start a terminal and any other subsequent terminal windows. But of course if I start another bash session via console the .bashrc will be read again and another ~/bin will be appended to the PATH. Like I said before on my Fedora computer I just export the PATH variable in the .bash_profile and it's read by every other new shell. Weird. |
It sounds like Ubuntu opens a terminal window differently to Slackware or Fedora. Can you open the menu editor and check what command is being used to invoke the terminal window?
|
Paths should be added to your profile files and not the bashrc files. You have to log out after adding a new path or source the profile file where you added the path e.g. "source ~/.bash_profile" if you do not want to log out.
|
Quote:
(I'm a perpetual newbie). When I open alacarte and go to terminal the command is "gnome-terminal" to open a terminal window. I do source my .bash_profile and .bashrc files after editing them and still have same issues. |
Quote:
|
Quote:
Here is a part of my .bash_profile. The little shell script code is default for Ubuntu: # set PATH so it includes user's private bin if it exists if [ -d ~/bin ] ; then PATH=~/bin:"${PATH}" fi export PATH Am I right to put the "export PATH" right after the shell code? |
It looks like there are 2 things happening with Ubuntu. Firstly, when you login at the GUI, you don't get what I consider to be a login - your .bash_profile and .bashrc files are not being sourced. I have my boxes set to a default run level with no GUI, so I don't know if that's typical of GUI logins. Secondly, your gnome-terminal is not starting as a login shell.
You can change the second by opening a gnome-terminal window and going to Edit / Profiles - select the Default profile and click the Edit button. On the Title and Command tab, click the check box for Run command as a login shell. Your .bash_profile file should be sourced the next time you open a gnome-terminal. |
That did the trick! Thanks alot gilead. I really appreciate you spending the time to solve this.By checking the "Run command as a login shell" box and restarting a terminal now I have ~/bin in the terminal and any other new terminal I start. It also works right when I log in a console.
I was posting this question on the Ubuntu Forums and they were telling me to put my PATH in the .bashrc file. But that just didn't sound right to me. I figured that as long as the variable is exported it should be available to any other new shell. Thanks. |
That's good news - I'm glad it's working the way you want it to now :)
|
All times are GMT -5. The time now is 04:54 AM. |