Sort out where to put my bash commands, alias, and configurations

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?

dotancohen

Posted 2013-05-10T12:28:11.183

Reputation: 9 798

2As a first step, you could just set .bashrc to echo 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.230

I 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 run tmux if it exists, otherwise .profile is run, .bashrc is run when I open a new Terminal in X or run screen. – 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

Answers

1

Probably the easiest way is to look at what environment variables are set when you log in via the different methods, then, using the table that Daniel Beck suggested, set up conditional statements in the appropriate .bash* file. For example, when I log in to my web server via SSH, variables like SSH_TTY are set, whereas they aren't when I log in to my home box from X.

if [ "$SSH_TTY" ]; then
    echo "SSH_TTY is $SSH_TTY"
else
    echo "You are logged in locally"
fi 

You can find out which variables are set by running export and env from the command line. You can also run set, but if you have rvm (the Ruby Version Manager) installed you'll get the entire contents of the script, so use a pager like less.

MattDMo

Posted 2013-05-10T12:28:11.183

Reputation: 4 968

The problem is that not all environments (distros) work the same. I will look at the environment settings, that might be helpful. – dotancohen – 2013-05-10T14:17:52.173

@dotancohen - right, that's why I suggested running export, env, and set, so you can see what exists when. Good luck! – MattDMo – 2013-05-10T14:19:48.533