改变 tmux .bash_profile 行为

6 shell login tmux

我的 .bash_profile 中有一个脚本,提示 X 会话启动。当我启动 tmux 时,我得到这个提示,我只打算用于 TTY 登录。

如果 .bash_profile 是 tmux 的一部分,那么我可以在 .bash_profile 中放入一些东西吗?也就是说,如果 .bash_profile 在 tmux 中被读取,我可以用 bash 检查吗?

J.C*_*ski 12

tmux 设置了一个名为 的环境变量$TMUX,我相信它保存了它正在使用的套接字的位置。无论哪种方式,您都可以在您.bash_profile的测试中使用它来测试它是否在 tmux 中被调用。

if [ -z "$TMUX" ]; then
    # not in tmux, do non-tmux things
fi
Run Code Online (Sandbox Code Playgroud)

或者

if [ -n "$TMUX" ]; then
    # called inside tmux session, do tmux things
fi
Run Code Online (Sandbox Code Playgroud)

  • 这有什么缺点吗?我认为 tmux 不应该将 shell 作为登录 shell 启动,虽然我在其他地方读到了关于它的激烈辩论,但我仍然没有看到令人信服的理由。我将在我的配置文件中使用这个 `$TMUX` 检查,如果没有其他原因,除了减少 `$PATH` 重复,但有什么缺点吗? (2认同)