为会话设置 TMUX 环境变量 [英] TMUX setting environment variables for sessions

查看:45
本文介绍了为会话设置 TMUX 环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在有多个项目的情况下工作,每个项目中都有许多脚本,这些脚本使用设置为该项目特定值的环境变量.

I work in a situation where I have multiple projects and within each are many scripts that make use of environment variables set to values specific to that project.

我想做的是为每个项目使用单独的 tmux 会话并设置变量,以便为该会话中的所有窗口设置它们.

What i'd like to do is use a separate tmux session for each project and set the variables so that they are set for all windows in that session.

我尝试使用 set-environment 选项,该选项使用 -g 选项工作,但随后为连接到该服务器的所有会话设置了变量.

I tried to use the set-environment option which works using the -g option but then sets the variable for all sessions connected to that server.

如果没有 -g 选项,我会在使用 show-environment 时看到它的设置,但无法访问 shell 中的变量.

Without the -g option I see its set when using show-environment but can't access the variable in the shell.

有没有人想出解决这个问题的方法?

Has anyone come up with a way of fixing this?

使用 tmux 1.8 和 tcsh

Using tmux 1.8 and tcsh

推荐答案

我想出了一个办法.我使用的是 tmux 2.5.

I figured out a way to do this. I'm using tmux 2.5.

背景

在 tmux 手册页中,它指出有两组环境变量:全局和每个会话.当您创建一个新的 tmux 会话时,它会将两个组合并在一起,并成为会话中可用的一组环境变量.如果将环境变量添加到全局组,则它们似乎在所有打开的会话之间共享.您想将它们添加到每个会话组.

In the tmux man page, it states that there are two groups of environment variables: global and per-session. When you create a new tmux session, it will merge the two groups together and that becomes the set of environment variables available within the session. If the environment variables get added to the global group, it appears that they get shared between all open sessions. You want to add them to the per-session group.

这样做

第 1 步:创建 tmux 会话.

Step 1: Create a tmux session.

tmux new-session -s one

第 2 步:将环境变量添加到每个会话组.

Step 2: Add an environment variable to the per-session group.

tmux setenv FOO foo-one

这会将环境变量添加到每个会话的环境变量集中.如果您键入 tmux showenv,您将在输出中看到它.但是,它不在当前会话的环境中.输入 echo $FOO 不会给你任何东西.可能有更好的方法来做到这一点,但我发现手动导出最简单:

This adds the environment variable to per-session set of environment variables. If you type tmux showenv, you'll see it in the output. However, it isn't in the environment of the current session. Typing echo $FOO won't give you anything. There's probably a better way to do this, but I found it easiest to just export it manually:

export FOO='foo-one'

第 3 步:创建新的窗口/窗格

Step 3: Create new windows/panes

现在,每次在当前会话中创建新窗口或窗格时,tmux 都会从每个会话组中获取 FOO 环境变量.

Now, every time you create a new window or pane in the current session, tmux will grab the FOO environment variable from the per-session group.

自动化

我使用 bash 脚本自动创建利用这些环境变量的 tmux 会话.下面是我如何自动执行上述操作的示例:

I use bash scripts to automatically create tmux sessions that make use of these environment variables. Here's an example of how I might automate the above:

#!/bin/bash
BAR='foo-one'
tmux new-session -s one \; \
  setenv FOO $BAR \; \
  send-keys -t 0 "export FOO="$BAR C-m \; \
  split-window -v \; \
  send-keys -t 0 'echo $FOO' C-m \; \
  send-keys -t 1 'echo $FOO' C-m

这篇关于为会话设置 TMUX 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆