2
2
It seems to me that every Linux distro that I SSH into or use locally (Mostly RHEL/CentOS and Debian/Ubuntu machines) have a different idea about which of .bashrc
and .bash_profile
to run when I log in locally, when I SSH in, when I open tmux, or when running shell commands from within VIM.
I do have some scripts that I would like to run when I log in, be that via SSH or on the console. For instance, I would like to see the output of uptime
, free
, and df
when I log in. I might log in from the computer itself (a Kubuntu desktop in a KDE session via Konsole), or I might SSH in from home to the same machine. However, these scripts should not run when e.g. running a shell command from within VIM. Should those be in .bashrc
or .bash_profile
?
Likewise, I have some aliases that I would like made available from the CLI no matter if I logged in from the local machine (KDE and Konsole) or SSHed in. These should also be available from with VIM when running shell commands, and also in tmux. Should those be in .bashrc
or .bash_profile
?
Edit: It seems that checking environment variables might help, using IF
conditions setting the things that I need. I would then put all the configurations into a third file .bash_dotan
, and then source that file from both .bashrc
and .bash_profile
to ensure that it is always run. I could wrap the whole thing in an IF [ALREADY_RUN]
conditional to ensure that it won't run twice if both .bashrc
and .bash_profile
are sourced. Are there any problems with this approach that I am not anticipating?
2As a first step, you could just set
.bashrc
toecho bashrc
, likewise with.bash_profile
, draw a nice table (servers as columns, method of opening a shell as row), and fill in which gets executed when. – Daniel Beck – 2013-05-10T13:11:14.230I think you may be asking the wrong question slightly, as .bash_profile is used for login shells and .bashrc is for interactive, non-login shells. The files aren't split by whether it is an ssh or local login. – Autumnal – 2013-05-10T13:12:45.233
That's really a good questions, and as @DanielBeck wrote,
echo
is the way to debug this. For me,.bash_profile
is executed when I log in via SSH or runtmux
if it exists, otherwise.profile
is run,.bashrc
is run when I open a new Terminal in X or runscreen
. – Stefan Seidel – 2013-05-10T13:16:09.957@ohope5 There might be a way to include what gets opened when, e.g. set in the Terminal emulator to explicitly run
bash --login
. But the first step is determining what runs when, especially as it appears that it depends on the distribution (perhaps due to a preference like the one I mentioned). – Daniel Beck – 2013-05-10T13:33:32.670@DanielBeck: Using
echo
is exactly how I figured out what was being sourced, when. – dotancohen – 2013-05-10T14:14:54.380@ohope5: You mention the intended purposes of the files, but empirically not every distro agrees on the distinction. – dotancohen – 2013-05-10T14:16:24.637