July 25, 2005
.bash_profile vs. .bashrc
One of the things I always have trouble remembering when working with linux is what is the correct ".profile" to edit when I want to automatically set environmental variables and such for my shell.
Included in Debian Woody are both .bash_profile, and .bashrc. I can never remember the difference between these two.
According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
What I take this to mean is that when I login when using a console, either physically at the machine or using ssh, .bash_profile is executed.
However, if I launch a terminal within a windowing system such as KDE, launch the Emacs *shell* mode, or execute /bin/bash from within another terminal then .bashrc is executed.
At any rate, the point is generally moot because most people edit the files so one calls the other anyway.
To do this under Debian Woody, we open .bash_profile and uncomment the following lines (under the comment # include .bashrc if it exists):
if [ -f ~/.bashrc ]; then source ~/.bashrc fi
Now when we login to our machine from a console, .bashrc will get called. As a bonus, in Debian Woody, the defualt .bashrc turns on coloring when running the ls command. Nice!
Posted by Josh Staiger at 07:13 PM
Comments
Posted by: Mahendra Shelke October 12, 2006 02:03 AM | Permanent link
Thank you - it really helped me
Posted by: Gene November 1, 2006 04:43 PM | Permanent link
I am really enlightened.
Posted by: shamima January 14, 2007 12:59 AM | Permanent link
This may give undesirable results should you run a remote (non-interactive) shell-calling program (e.g.: scp). It's probably wisest to use these .rc files as they were designed to be used. Alas, I'm not exactly sure how they were designed to be used. :-P I use tcsh, so I copied all of my old .tcshrc file into .tcsh_profile and combined the calls to .cshrc and .alias into .tcsh_profile while I was at it. Now when I login I don't get any of my settings, but at least scp works again. Ah "progress"!
Posted by: Anonymous February 4, 2007 03:03 AM | Permanent link
I've been calling .bashrc from .bash_profile on my remote machine for a while and have never had a problem with scp, but I suppose it depends on the content of those files.
Posted by: Josh Staiger February 4, 2007 07:50 PM | Permanent link
Thanks a lot! This issue has been gnawing me for months!
Posted by: Ankit Agrawal March 1, 2007 04:34 AM | Permanent link
Wouldn't this cause problem if you run one script from the other in both of them? For example, if you have those 3 lines to include .bashrc in .bash_profile, then it starts executing .bashrc and then reaches those 3 lines in .bashrc and starts to execute .bash_profile. Doesn't this result in some sort of recursive stack overflow?
Posted by: Tom April 16, 2007 06:25 PM | Permanent link
I absolutely despise coloring ls output. ls -F is the obviously correct solution.
Posted by: ajv April 16, 2007 08:00 PM | Permanent link
It's OK to call .bashrc from .bash_profile as long as you're careful what commands you put in .bashrc. However you should never call .bash_profile from .bashrc. If you really want to execute .bash_profile, just start bash with a '-l' (or a '--login' if you prefer) option.
Posted by: DevilDogs April 16, 2007 09:32 PM | Permanent link
I absolutely despise coloring ls output. ls -F is the obviously correct solution.
Well, that's a personal preference. But ls -F is horrible IMO: those tacked on characters make files look like they have wonky names, to the extent that novice users even try to type them in as part of the name (which has caused me tech support hassle in the past). Coloring produces no such confusion- you don't get people confused because they can't type a green filename or purple filename, wanting to because "that's how it looks in the listing"...
Posted by: Spissy Spispopd April 16, 2007 10:38 PM | Permanent link
Certain terminal emulators can be set to execute login shells rather than regular shells. And this has security benefits, IIRC, with regards to things that get logged to the system log (mostly to do with su/sudo, I'd have to look it up).
.bashrc is also run whenever you run a script written in bash. So if you want to have /usr/games/fortune spit you out a funny bit of wisdom, you should make it run in .bash_profile, NOT .bashrc, unless you want to see a fortune every time an sh process gets directly or indirectly executed by a script (I did this once, and man, compiling packages from source got a little more interesting at least).
Posted by: David Warde-Farley April 17, 2007 04:56 AM | Permanent link
You should use ~/.profile for setting environment variables or executing login scripts. That way if you decide to switch to another shell (or if bash is for some reason unavailable - due to a disk failure for example) you won't need to move those scripts out of ~/.bash_profile - they'll be appropriately executed from ~/.profile.
Any bash-specific settings should be set in ~/.bashrc, so that they will be set even if you execute bash from another shell or xterm (ie if bash is not your login shell for that session).
All of this is clearly stated in the bash man page (ie 'man bash' at the command line), as well as the user guide.
Posted by: Andrew April 17, 2007 07:09 AM | Permanent link
It's OK to call .bashrc from .bash_profile as long as you're careful what commands you put in .bashrc.
It is not just OK - it is standard/typical behaviour as stated by the user guide.
However you should never call .bash_profile from .bashrc.
Especially if you're already calling .bashrc from .bash_profile. ;-)
Posted by: Andrew April 17, 2007 07:13 AM | Permanent link
This finally explained why I never saw my ~/.bashrc settings when I would login interactively.
I tested the if/then contruct in my ~/.bash_profile and, sure enough, I get my fancy PS1 settings and all my aliases.
Posted by: Bill April 17, 2007 07:51 AM | Permanent link
Cool! I never look at this before :)
Posted by: Planet Malaysia` April 17, 2007 10:26 AM | Permanent link
Why not just hardlink .bashrc to .bash_profile and have one file to edit?
Posted by: Robert D. April 17, 2007 01:02 PM | Permanent link
I typically check .bash_profile for anything useful. If there's anything in there I want to keep, I move the contents to .bashrc. I then symlink .bash_profile to .bashrc to keep things simple.
I also used to use the `source .bashrc` method you mentioned, but in the end I found it simpler to have only one file to maintain.
Posted by: hollywoodb April 17, 2007 06:17 PM | Permanent link
Thanx for enlightning , i was mystefied on this front ..