Ubuntu – .profile and .bash_profile ignored when starting tmux from .bashrc

.bash-profilebashrcprofiletmuxUbuntu

I've been having an issue with getting my .profile to be sourced when SSHing into an Ubuntu 12.04 server instance. My .bashrc does the following towards the end:

if [[ "$TERM" != "screen-256color" ]]
then
   tmux attach-session -t "$USER" || tmux new-session -s "$USER"
   exit
fi

If I comment out that code block and log in with ssh again, the ~/.profile file is sourced as expected. There is no .bash_profile or .bash_login under ~ which would prevent ~/.profile from being read. Renaming .profile to .bash_profile as an experiment also didn't get the file sourced.

Does anybody have a guess for why that might be happening? I could just stuff everything into .bashrc, but I'd love to find out why .profile isn't being sourced.

Best Answer

As discussed in Why ~/.bash_profile is not getting sourced when opening a terminal? on “Ask Ubuntu”, the shell running under tmux is not a login shell.

How can I tell whether a shell is a “login shell”?

  1. Try typing “logout”.  If the shell terminates, it was a login shell.  If it says it isn’t a login shell, then it isn’t.
  2. Type “ps -fp$$”.  (Modify, if your ps takes different args, to do whatever you need to in order to get a full/long listing of process information for the current shell ––  “–p” means “look at this process”, and “$$” is the PID of the shell.)  If the process name begins with a dash (hyphen), as in “-bash” or “-csh”, it’s a login shell; otherwise, it isn’t.  (Probably.)

… so, the shell running under tmux will not look at .bash_profile; it will look only at .bashrc.  So move the commands that you want executed from .bash_profile to .bashrc, or put them both places, or make one source the other.

Related Question