.bashrc/.profile is not loaded on new tmux session (or window) — why?

会有一股神秘感。 提交于 2019-11-27 10:12:30

问题


When tmux starts or opens a new window, it does not load my .profile or .bashrc. I end up typing . ~/.bashrc every time. Is there a way to make this happen automatically?


回答1:


Yes, at the end of your .bash_profile, put the line:

. ~/.bashrc

This automatically sources the rc file under those circumstances where it would normally only process the profile.

The rules as to when bash runs certain files are complicated, and depend on the type of shell being started (login/non-login, interactive or not, and so forth), along with command line arguments and environment variables.

You can see them in the man bash output, just look for INVOCATION - you'll probably need some time to digest and decode it though :-)




回答2:


Running bash explicitly worked for me, by adding this line to my ~/.tmux.conf file:

set-option -g default-command "exec /bin/bash"



回答3:


From this thread:

  • https://bbs.archlinux.org/viewtopic.php?id=124274

seems using .bash_profile would work.




回答4:


The solution that worked for me is the following:

  • Create a .bash_profile file if you don't have one in ~
  • At the end of .bash_profile put source ~/.bashrc or source ~/.profile
  • Restart tmux.

The issue should now be fixed.




回答5:


Former answers provided solutions but didn't explain the reason. Here it is.

This is related to the Bash init files. By default, ~/.bashrc is used in an interactive, non-login shell. It won't be sourced in a login shell. Tmux uses a login shell by default. Hence, shells started by tmux skip ~/.bashrc.

default-command shell-command

The default is an empty string, which instructs tmux to create a login shell using the value of the default-shell option.

Init files for Bash,

  1. login mode:
    1. /etc/profile
    2. ~/.bash_profile, ~/.bash_login, ~/.profile (only first one that exists)
  2. interactive non-login:
    1. /etc/bash.bashrc (some Linux; not on Mac OS X)
    2. ~/.bashrc
  3. non-interactive:
    1. source file in $BASH_ENV

The weird interactive, non-login loading requirement confuses people in other situations as well. The best solution is to change the loading requirement of ~/.bashrc as interactive only, which is exactly what some distros, like Ubuntu, are doing.

# write content below into ~/.profile, or ~/.bash_profile

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

This should be the solution you desire. And I recommend every Bash user setup this in the profile.

References

  • Unix Shell Initialization
  • man tmux



回答6:


I had the same problem and the solutions so far didn't work for me. The solution that ended up working for me can be found here.

In short, tmux windows/sessions use a login shell which looks for a ~/.profile among other files when it starts.

What I wanted was to have zsh start with each new tmux window so I put exec zsh at the bottom of my ~/.profile.



来源:https://stackoverflow.com/questions/9652126/bashrc-profile-is-not-loaded-on-new-tmux-session-or-window-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!