Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
2  
As 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 May 10 '13 at 13:11
    
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. –  ohope5 May 10 '13 at 13:12
    
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 May 10 '13 at 13:16
    
@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 May 10 '13 at 13:33
    
@DanielBeck: Using echo is exactly how I figured out what was being sourced, when. –  dotancohen May 10 '13 at 14:14

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
The problem is that not all environments (distros) work the same. I will look at the environment settings, that might be helpful. –  dotancohen May 10 '13 at 14:17
    
@dotancohen - right, that's why I suggested running export, env, and set, so you can see what exists when. Good luck! –  MattDMo May 10 '13 at 14:19

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.