LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-24-2007, 04:59 PM   #1
nick623
Member
 
Registered: Dec 2004
Distribution: Fedora, Ubuntu
Posts: 83

Rep: Reputation: 15
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.
 
Old 03-24-2007, 05:17 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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...
 
Old 03-24-2007, 06:16 PM   #3
nick623
Member
 
Registered: Dec 2004
Distribution: Fedora, Ubuntu
Posts: 83

Original Poster
Rep: Reputation: 15
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?

Last edited by nick623; 03-24-2007 at 07:13 PM.
 
Old 03-24-2007, 08:47 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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.
 
Old 03-25-2007, 08:37 AM   #5
nick623
Member
 
Registered: Dec 2004
Distribution: Fedora, Ubuntu
Posts: 83

Original Poster
Rep: Reputation: 15
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.

Last edited by nick623; 03-25-2007 at 07:59 PM.
 
Old 03-28-2007, 09:50 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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/pathriginal/path or worse.

One last thought, in your .bashrc, does it preserve your original path, or is the statement something like export PATH=~/bin?
 
Old 03-30-2007, 03:42 PM   #7
nick623
Member
 
Registered: Dec 2004
Distribution: Fedora, Ubuntu
Posts: 83

Original Poster
Rep: Reputation: 15
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.
 
Old 03-30-2007, 04:29 PM   #8
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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?
 
Old 03-30-2007, 04:51 PM   #9
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
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.
 
Old 03-30-2007, 06:15 PM   #10
nick623
Member
 
Registered: Dec 2004
Distribution: Fedora, Ubuntu
Posts: 83

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by gilead
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?
By "opening a menu editor" I guess you mean something like 'alacarte'
(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.
 
Old 03-31-2007, 02:07 AM   #11
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Quote:
I do source my .bash_profile and .bashrc files after editing them and still have same issues.
When you source your profile files, they only change paths for that particular terminal or virtual terminal session. If you want the paths to apply to all terminal sessions (and maybe a GUI environment like KDE), then you need to log out and back in again. Paths are not supposed to be in bashrc as you have noticed because each time you start a terminal, your path keeps being appended with the same directories.
 
Old 03-31-2007, 08:42 AM   #12
nick623
Member
 
Registered: Dec 2004
Distribution: Fedora, Ubuntu
Posts: 83

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by reddazz
When you source your profile files, they only change paths for that particular terminal or virtual terminal session. If you want the paths to apply to all terminal sessions (and maybe a GUI environment like KDE), then you need to log out and back in again. Paths are not supposed to be in bashrc as you have noticed because each time you start a terminal, your path keeps being appended with the same directories.
I've tried logging in and out several times with the exported PATH in the .bash_profile file but ~/bin is still not available when I open any terminal window. It' only available when I open a console via Ctrl-Alt-F1 etc.

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?
 
Old 03-31-2007, 04:08 PM   #13
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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.
 
Old 03-31-2007, 05:17 PM   #14
nick623
Member
 
Registered: Dec 2004
Distribution: Fedora, Ubuntu
Posts: 83

Original Poster
Rep: Reputation: 15
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.
 
Old 03-31-2007, 06:41 PM   #15
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
That's good news - I'm glad it's working the way you want it to now
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bashrc vs. bash_profile mikeshn Linux - General 9 02-10-2008 01:46 PM
bashrc and .bash_profile happy78 Programming 5 10-19-2005 04:26 PM
bashrc or bash_profile ? ARCIS_house Linux - Newbie 6 07-21-2005 02:09 PM
.bashrc .bash_profile Soulful93 Slackware 4 05-03-2005 03:02 AM
.bashrc vs .bash_profile xor3 Linux - Newbie 1 08-16-2003 12:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration