I am using tmux often (and chiefly) when working remotely in a text-only SSH pty; it's very useful in case a connection drops. I run long (hours to days) computation tasks, and connect from different remote hosts to check on it. This is nearly the only thing I do with tmux; I am far from being a pro in it, so my question may possibly be very basic, but I cannot find an answer.
I have a command on the host that reads and parses out the client address from the the
$SSH_CONNECTION shell variable to communicate (directly, no ssh tunnel) with a daemon on my current client machine in a certain way¹. This breaks under tmux when I attach to a session initiated originally on another machine. The bottom-most shell in the tmux session has already captured the variable on its startup, and has the address of the client machine that has initiated the session, not one that it is currently attached to it.
A simple solution would be to reset this variable altogether in my
.bashrc, so the tool does not even attempt to communicate with the client connection inside a tmux sesstion (I can give that tool an IP in its command line, after all, if it fails to guess one). I believe I can rely on the presence of the
$TMUX env variable to avoid capturing the IP address altogether in my .bashrc.
But The Right Thing would be to somehow lift this variable from the bash in which I execute the
tmux attach command. I understand it cannot be fully automatic, as tmux on the remote likely could not modify the environment of the already running first shell. But maybe I can use some fancy tmux scripting to pull environment variables from the
tmux process that had run the
attach command (the attaching SSH session machine has them set correctly in its environment), store the values somewhere in the session tmux variables (do they even exist?), and let me run a custom command to unget (i. e., fake typing into the session's bash
export SSH_SESSION=....) with a keystroke when I know it's safe, i. e. on the bash prompt inside the session?
TL;DR: I want a way to set an env variable within a session that I attach to based on on an env variable value in a another pty connection in which I run the
tmux attach command, in the least cumbersome way, like a good-skilled tmuxer would.
¹ This not a good or secure way to do it, but it's how it is. We have to use a lot of weird stuff in scientific computation, written by fellow scientists, who are, or some of them already were, as in this case, not necessarily good software engineers. I'd do it differently in this century, but this thing is like 30 years old.