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
source share