How does tmux spawn multiple sessions?
There is a section named ENVIRONMENT
in man 1 tmux
:
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.The
update-environment
session option may be used to update the session environment from the client when a new session is created or an old reattached. […]Commands to alter and view the environment are:
set-environment
[…]
show-environment
[…]
And where the manual explains set-option
, it reads:
update-environment variables
Set a space-separated string containing a list of environment variables to be copied into the session environment when a new session is created or an existing session is attached.
[…] The default is
"DISPLAY SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY"
.
So your a
and b
are copied to tmux
only when the server starts. In your case it's when you create the first
session. These variables are not on the list stored by update-environment
option, so later they are not updated.
If you did
tmux set-option -t second update-environment "a b"
and then attached to this session, your current variables would be absorbed by tmux
. This doesn't mean echo $a $b
would show them in the shell that had already been started (by tmux new-session -d -s first
). But a new shell (or any other process) in a new pane would inherit them from tmux
.
It's possible to set the option globally. See the OPTIONS
section. But keep these things in mind:
- the option is designed to work when a new session is created or an old reattached (although you can change the behavior with
-E
option ofattach-session
; few other commands also support-E
); - the updated value won't affect existing processes (panes).
To summarize: in general tmux
sessions and processes within don't simply inherit the environment from the client like a simple child process would do.