.profile und .bash_profile werden ignoriert, wenn tmux von .bashrc gestartet wird?

6400
glitch

Ich hatte ein Problem damit, dass mein .profile beim SSHing in eine Ubuntu 12.04-Serverinstanz bezogen wird. Meine .bashrc tut gegen Ende Folgendes:

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

Wenn ich diesen Codeblock auskommentiere und mich erneut mit ssh anmelde, wird die Datei ~ / .profile erwartungsgemäß bezogen. Es gibt keine .bash_profile oder .bash_login unter ~, die das Lesen von ~ / .profile verhindern würden. Durch das Umbenennen von .profile in .bash_profile als Experiment wurde die Datei auch nicht abgerufen.

Hat jemand eine Vermutung, warum das passieren könnte? Ich könnte einfach alles in .bashrc stecken, aber ich würde gerne herausfinden, warum .profile nicht beschafft wird.

4
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 vor 10 Jahren 0
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 vor 10 Jahren 0
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](http://http://unix.stackexchange.com/), you might have better luck there. terdon vor 10 Jahren 1
Guter Vorschlag, danke. Ich lasse das hier ein bisschen sitzen und bewege es, wenn nichts passiert. glitch vor 10 Jahren 0
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?](http://askubuntu.com/questions/121073/why-bash-profile-is-not-getting-sourced-when-opening-a-terminal) on Ask Ubuntu. Scott vor 10 Jahren 2
@Scott, du hast recht, es ist tatsächlich keine Login-Shell. Alle Ihre Lösungen scheinen gültig zu sein. glitch vor 10 Jahren 0

1 Antwort auf die Frage

8
Scott

Wie in Warum beschrieben, wird ~ / .bash_profile beim Öffnen eines Terminals nicht verwendet? Bei "Ask Ubuntu" ist die Shell, unter der ausgeführt tmuxwird, keine Login-Shell.

Wie kann ich feststellen, ob eine Shell eine "Login-Shell" ist?

  1. Versuchen Sie, " logout" einzugeben . Wenn die Shell beendet wird, handelt es sich um eine Login-Shell. Wenn es sagt, dass es keine Login-Shell ist, dann nicht.
  2. Geben Sie " ps -fp$$" ein. (Ändern Sie, wenn Sie Ihre psverschiedenen args nimmt, zu tun, was Sie brauchen, um eine vollständige / lange Liste von Prozessinformationen für die aktuelle Shell zu bekommen - „ –p“ bedeutet „betrachten diesen Prozess “ und „ $$“ ist die PID die Shell.) Wenn der Prozessname wie in „ -bash“ oder „ -csh“ mit einem Bindestrich (Bindestrich) beginnt, handelt es sich um eine Login-Shell. sonst ist es nicht. (Wahrscheinlich.)

… Also tmuxsieht die unterlaufende Shell nicht aus .bash_profile; es wird nur schauen .bashrc. Verschieben Sie also die Befehle, von denen Sie ausführen möchten, .bash_profilean .bashrcbeide Orte oder stellen Sie eine Quelle zur anderen.