1

I have a bash script:

#!/bin/bash
export TEST=$1
tmux new-session -d -s $1
tmux send-keys -t $1:0 "printenv | grep TEST" C-m

Run:

# ./test.sh 123  #The tmux server is automatically launched
# ./test.sh 555

Results:

  • tmux a -t 123 shows TEST=123
  • tmux a -t 555 shows TEST=123 instead of TEST=555!!!!

Why does my next TMUX sessions inherit all the environment variables of the first created session?

That's quite surprising, how can I do to stop this non-sense behavior?

https://manpages.debian.org/experimental/tmux/tmux.1.en.html#GLOBAL_AND_SESSION_ENVIRONMENT

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.

When I first run 123, TEST is set GLOBAL for the tmux server. But when I run 555, TEST=123 should be replaced by the session env, but it isn't...

1

1 Answer 1

2

As far as I understand reading the manual, this is the intended behavior.

When the server is (automatically) launched when you create your first session, the global environment is indeed created and get the TEST variable.

Why do you think that when you create the second session, the session environment will get TEST.

To set a session environment, you can use set-environment.

#!/bin/bash

tmux new-session -d -s $1
tmux set-environment -t $1 TEST $1

run it

./test.sh 123
./test.sh 555

then you can look an environment using show-environment

tmux show-environment -t 123 TEST
tmux show-environment -t 555 TEST

EDIT

Window 0 created at new-session and thus does not get env set using set-environment.

I agree this is weird.

I think what you want can be achieve with (something like) this is .bashrc:

if [ -n "$TMUX" -a -n "$ENVFILE" ]; then
    source $ENVFILE
fi

and newsess.sh:

#!/bin/bash

sname=`basename $1`
tmux new-session -d -s $sname "ENVFILE=$1 bash"
tmux set-environment -t $sname ENVFILE $1

/tmp/env:

export A=1
export B=2

/tmp/env2:

export A=5
export B=5

then

./newsess.sh /tmp/env
./newsess.sh /tmp/env2

You got A=1 and B=2 in each window of session env and A=5 and B=5 in each window of session env2.

12
  • Because it's completely counterintuitive for linux dev, especially when coming from screen. It's such a weird design decision. The server has to be created at boot for a random session otherwise it might affect all the other sessions.
    – None
    Jan 23, 2022 at 10:33
  • In your solution, TEST is set after the first command is executed. I'd like to at least configure the session environment from an env file KEY=VALUE, without needing to list or send one command per variable.
    – None
    Jan 23, 2022 at 10:34
  • I meant "why do you think [...], according to the manual". What exactly are you trying to achieve? Can't you source a file or execute a script? Maybe update-environment?
    – yarl
    Jan 23, 2022 at 10:41
  • 1
    Right, I did not see about windows 0 automatically created and thus does not get session variable set after. I understand and agree now.
    – yarl
    Jan 23, 2022 at 12:45
  • 1
    Ok. I edited my answer.
    – yarl
    Jan 23, 2022 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .