5

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.

6
  • Are you sure it is not sourced? What happens if you add an echo foo > logfile or similar just to check whether it is sourced? I am thinking that the problem may be the (as far as I can tell, useless) exit in your snippet above.
    – terdon
    Commented Jul 1, 2013 at 18:21
  • terdon, without the exit you'd have to type exit (or use CTRL-D) twice two log out of an ssh session. With exit, the bash session closes immediately, meaning that when tmux is closed, the ssh connection is closed as well. Testing with the snipped you shared above confirmed that .profile is indeed not read by the tmux session (but it's actually sourced in the bash session that starts tmux)
    – glitch
    Commented Jul 1, 2013 at 18:30
  • 1
    Ah, OK, sorry, I don't use tmux so I have not encountered that. So you are saying that adding the if statement you have posted stops the file from getting sourced? Very strange... If you get no good answers here after a few days, you might want to flag the question for migration to U&L, you might have better luck there.
    – terdon
    Commented Jul 1, 2013 at 18:35
  • Good suggestion, thank you. I'll let this sit here for a bit and move it if nothing happens.
    – glitch
    Commented Jul 1, 2013 at 18:44
  • 2
    I guess the shell running under tmux is not a login shell (what happens if you type logout?), so that shell 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. See Why ~/.bash_profile is not getting sourced when opening a terminal? on Ask Ubuntu. Commented Jul 2, 2013 at 1:00

1 Answer 1

9

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .