Ubuntu – bashrc or bash_profile

bashscriptsstartup

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…)

Best Answer

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