TMUX session environment variables

I work in a situation where I have several projects, and in each of them there are many scripts that use environment variables for values ​​specific to this project.

What I would 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.

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

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

Has anyone come up with a way to fix this?

Using tmux 1.8 and tcsh

+6
source share
3 answers

You can access the tmux (local) environment variables for each session, and in a session, with the command:

bash> tmux show-environment 

If you add the -g option, you get an environment for all sessions, i.e. a global environment. Local environments do not match the global environment. The previous command prints the entire local environment, but you can also see only one variable:

 bash> tmux show-environment variable_name variable_name=value 

To get the value, you can use the magic of "sed" or use the "export" for a single variable, or you can even "export" the entire environment to your shell. Below are 3 approaches.

 bash> tmux show-environment variable_name | sed "s:^.*=::" value bash> eval "export $(tmux show-environment variable_name)" bash> echo $variable_name value bash> for var in $(tmux show-environment | grep -v "^-"); do eval "export $var"; done; bash> echo $variable_name value 

If necessary, you can simply add the -g option after the show-environment command if you want to access the global environment.

+4
source

I made a simple

 export MY_VAR="some value" 

before starting a tmux session, which gives me access to MY_VAR from all the windows inside this session.

+4
source

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

Background

The tmux man page indicates that there are two groups of environment variables: global and for each session. When you create a new tmux session, it will merge both groups together and become the set of environment variables available in the session. If environment variables are added to the global group, it seems that they are distributed among all open sessions. You want to add them to the group per session.

Do it

Step 1. Create a tmux session.

 tmux new-session -s one 

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

 tmux setenv FOO foo-one 

This adds an environment variable for each set of environment variables. If you type tmux showenv , you will see it on the output. However, it is not in the current session environment. Typing echo $FOO will not give you anything. Probably the best way to do this, but it was easier for me to just export it manually:

 export FOO='foo-one' 

Step 3. Creating new windows / panels

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

Automation

I use bash scripts to automatically create tmux sessions that use these environment variables. Here is an example of how I can automate the above:

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

All Articles