How to pass an environment variable from the currently attached client into a tmux session upon attach












0















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 e. g. export SSH_SESSION=....) wit 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.



I cannot find any pointers. I honestly used up all my google-fu! :)





¹ This not a good or secure way to do it, but it's how it is, please forgive me. 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.









share



























    0















    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 e. g. export SSH_SESSION=....) wit 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.



    I cannot find any pointers. I honestly used up all my google-fu! :)





    ¹ This not a good or secure way to do it, but it's how it is, please forgive me. 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.









    share

























      0












      0








      0








      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 e. g. export SSH_SESSION=....) wit 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.



      I cannot find any pointers. I honestly used up all my google-fu! :)





      ¹ This not a good or secure way to do it, but it's how it is, please forgive me. 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.









      share














      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 e. g. export SSH_SESSION=....) wit 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.



      I cannot find any pointers. I honestly used up all my google-fu! :)





      ¹ This not a good or secure way to do it, but it's how it is, please forgive me. 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.







      tmux





      share












      share










      share



      share










      asked 5 mins ago









      kkmkkm

      1615




      1615






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508863%2fhow-to-pass-an-environment-variable-from-the-currently-attached-client-into-a-tm%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508863%2fhow-to-pass-an-environment-variable-from-the-currently-attached-client-into-a-tm%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          宮崎県

          濃尾地震

          シテ島