新的 tmux 会话不提供 bashrc 文件

JWF*_*JWF 36 bash tmux

概括

当我创建一个新的 tmux 会话时,我的提示从默认的 bash 配置中提取,我必须手动运行source ~/.bashrc我的自定义提示。

分析

我正在使用 RHEL 7 机器。在不久前的一次 bash 更新后,我开始注意到这种行为,但直到现在才开始提出这个问题(我不确定这开始发生在哪个更新)。

例如,我已将提示自定义为如下所示:

[user@hostname ~]$

每当我开始一个新的 tmux 会话时,它使用的似乎是 bash 默认值:

-sh-4.2$

快速运行source ~/.bashrc总是可以解决问题,但每次我想修复一些小问题时都必须这样做,这很烦人。关于如何让 tmux 再次自动执行此操作的任何想法?

如果需要更多信息,我很乐意提供。

配置文件

作为参考,我在tmux.conf下面有我的文件,尽管它几乎不是您可以称之为自定义的。

setw -g mode-keys vi

# reload tmux.conf
bind r source-file ~/.tmux.conf \; display-message " ? ~/.tmux.conf is reloaded"
Run Code Online (Sandbox Code Playgroud)

Sim*_*mba 32

这与 Bash init 文件有关。默认情况下,~/.bashrc交互式非登录shell 中使用。它不会来自登录 shell。Tmux默认使用登录 shell。因此,shell 由 tmux skip 启动~/.bashrc

default-command 外壳命令

默认值是一个空字符串,它指示 tmux使用default-shell选项的值创建登录 shell

用于 Bash 的初始化文件,

  1. 登录方式:
    1. /etc/profile
    2. ~/.bash_profile, ~/.bash_login, ~/.profile(只有第一个存在)
  2. 交互式 非登录
    1. /etc/bash.bashrc (一些 Linux;不在 Mac OS X 上)
    2. ~/.bashrc
  3. 非交互式:
    1. 源文件在 $BASH_ENV

解决方案

奇怪的交互式非登录加载要求在其他情况下也让人们感到困惑。将最好的解决办法是改变的负载要求,~/.bashrc作为唯一的互动,这正是一些发行版,如Ubuntu,在做什么。

# 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
Run Code Online (Sandbox Code Playgroud)

这应该是您想要的解决方案。我建议每个 Bash 用户在配置文件中设置它。


更新:上述设置是从 Ubuntu 复制的。.bashrc无论是否在交互式 shell 中,他们似乎都选择在登录 shell 中加载。

如果您想检测交互式 shell,请使用$PS1.

# 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
Run Code Online (Sandbox Code Playgroud)

参考


and*_*tsh 30

据我所知,默认情况下tmux运行登录 shell。当bash被作为交互的登录shell,它查找~/.bash_profile~/.bash_login~/.profile。所以你必须放入source ~/.bashrc其中一个文件。

解决此问题的另一种方法是在您的文件中添加.tmux.conf以下行:

set-option -g default-shell "/bin/bash"
Run Code Online (Sandbox Code Playgroud)

  • macOS 用户注意:如果你已经通过 Homebrew 安装了 `bash` 并且这不起作用,请将 `"/bin/bash"` 更改为 `"/usr/local/bin/bash"`。 (3认同)

小智 9

将以下内容添加到.tmux.conf

set-option -g default-shell "/bin/bash"
Run Code Online (Sandbox Code Playgroud)

不会产生预期的结果。

只有当添加source "$HOME/.bashrc"~/.bash_profile预期的结果时才能实现。

这将在打开新窗口或窗格时以及在分离和打开新 tmux 会话时对活动的 tmux 会话起作用。

测试:

VERSION="16.04.2 LTS (Xenial Xerus)"
tmux 2.1
Run Code Online (Sandbox Code Playgroud)