tmux has an interesting quirk in the way it handles environment variables that if you’re not careful can cause some seemingly strange behavior.
Fortunately, this behavior is documented but in my opinion, is not intuitive. From the tmux man page:
When the server is started, tmux copies the environment into the
global environment; in addition, each session has a session
environment. When a window is created, the session and global
environments are merged. If a variable exists in both, the value
from the session environment is used. The result is the initial
environment passed to the new process.
What this says is that the global environment is copied over when the server is
started. Each session also has an environment that is merged with the original
global environment but it only has a set list of environment variables that are
updated. You can see this with the following command in a tmux session:
> tmux show-environment
-DISPLAY
-KRB5CCNAME
-SSH_AGENT_PID
-SSH_ASKPASS
SSH_AUTH_SOCK
-SSH_CONNECTION
-WINDOWIDkj
-XAUTHORITY
This can cause some interesting consequences when you have multiple named tmux sessions running. Here’s an example of
this in action:
>