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 know the difference between the two bash login scripts:

.bashrc is run only by "non-login" shells.

.bash_profile (or .bash_login or .profile) is executed by "login" shells.

Does anyone have some good examples of what things that are a better fit for login-only execution, such that I'd only put them in .bash_profile, but they wouldn't really make sense in .bashrc?

(I know most of us source .bashrc out of .bash_profile, so there doesn't seem to be much point in the opposite question...)

share|improve this question
1  
See also the same question at Super User. –  Gilles Sep 6 '10 at 19:41

2 Answers 2

up vote 19 down vote accepted

Since a .bashrc is for non-login shells, I avoid any commands which echo to the screen. I've also run into experiences where echo statements in .bashrc will cause sftp and rsync commands to fail (and maybe scp commands as well).

# Print some information as we log in
# -s: OS Name -n: Node name -r: OS Release
uname -snr
uptime

Also, you generally won't run ssh-agent from a non-interactive shell. So I have this in .bash_profile.

if [ -f ~/.ssh/ssh-agent ]; then . ~/.ssh/ssh-agent; fi
share|improve this answer
2  
If you use ~/.profile instead of ~/.bash_profile things will still work even if you change shells.. ~/.bash_profile is for bash specific things. –  Source Lab Aug 7 '10 at 8:55
1  
But bash will only run EITHER .bash_profile OR .profile, so if you're going to use both, you need to source .profile from within .bash_profile or something. That isn't a bad idea, actually... –  Don Faulkner Aug 9 '10 at 14:43
    
@Source & @Don : Good points. I used to have Bashisms in my .bash_profile, but now I've switched to something more universal. Maybe using .profile is in order. –  Stefan Lasiewski Aug 9 '10 at 23:26

Byobu is a great example of something you should never ever put in a .bashrc.

Otherwise, it will recursively run itself in every single one of its 'virtual terminals' ;-) You can try it though, it's sort of fun.

That why you put it in .profile, so byobu (which really is a just wrapper around screen) is only loaded, once, at login-time. And byobu itself can start new interactive bash sessions.

share|improve this answer
    
That's a good example. Thanks for the pointer to Byobu as well. Now to go try it out. –  Don Faulkner Sep 7 '10 at 13:12

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.