如何将Tmux配置为加载~/.bashrc而不是~/.bash_profile?

10

现在,当我登录到Tmux时,仅加载~/.bash_profile

我希望调用~/.bashrc

这可行吗?


3
可能是与".bashrc/.profile is not loaded on new tmux session (or window) -- why?"问题重复。 - Sylvain Leroux
2个回答

8
你可以通过在 tmux 级别显式设置默认 shell 命令来解决这个问题:
tmux set-option default-command "/bin/bash"

来自tmux手册(重点是我的):

         default-command shell-command
                 Set the command used for new windows (if not specified
                 when the window is created) to shell-command, which may
                 be any sh(1) command.  The default is an empty string,
                 which instructs tmux to create a **login shell** using the
                 value of the default-shell option.
         default-shell path
                 Specify the default shell.  This is used as the login
                 shell for new windows when the default-command option is
                 set to empty, and must be the full path of the exe‐
                 cutable.  When started tmux tries to set a default value
                 from the first suitable of the SHELL environment vari‐
                 able, the shell returned by getpwuid(3), or /bin/sh.
                 This option should be configured when tmux is used as a
                 login shell.
如Chepner在下面的评论中所解释的那样:
"default-shell"默认为您喜欢的登录Shell; "default-command"默认为启动登录实例,有效地为"$SHELL -l"
... 而在Bash中,一个"登录"Shell不会读取~/.bashrc。 通过覆盖"default-command"的值,我们现在可以强制"tmux"创建一个"非登录"Shell。

在这种情况下,您不需要使用“-i”来启动交互式 shell,因为 shell 的标准输入/错误将被绑定到一个(伪)终端。 - chepner
@chepner 确实;在答案中已经修复。 但是我不明白为什么将默认命令显式设置为“/bin/bash”可以工作,但如果我让默认值不起作用。即使我的$SHELL和passwd条目都设置为/bin/bash。根据我答案中引用的相关部分的man,它不应该有任何区别。或者我错过了什么? - Sylvain Leroux
1
default-shell 默认使用您首选的登录 shell;default-command 默认启动一个登录实例,实际上是 $SHELL -l - chepner
在XUbuntu 18.04 LTS和tmux 2.6中,使用命令行启动tmux无法正常工作。难道它不是应该在命令行中运行吗? - Horror Vacui

8
这个问题与tmux无关。要解决该问题,确保在.bash_profile中添加source ~/.bashrc即可。
您可以在此处了解有关bash初始化及其加载文件的更多信息,以及加载顺序:https://github.com/sstephenson/rbenv/wiki/Unix-shell-initialization#bash 从列表中可以看出,在登录模式下启动bash时,.bashrc甚至不列在列表中,这就是为什么我们从列表中的文件(.bash_profile)源代码。

手册页的调用部分将更好地参考哪些文件在启动时使用。 - chepner
这对我不起作用,我已经在我的.bash_profile中引用了我的.bashrc,但tmux没有读取它。 - asa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接