Passing variables into ssh sessions (Session has to stay alive)

8,062

Solution 1

I think you can try this:

ssh user@host -t 'export var="value"; bash' 

Solution 2

ssh user@host -t 'export var="value"; bash -login'

Add -login to make sure whole env is properly setup.  

Share:
8,062

Related videos on Youtube

Bembelz
Author by

Bembelz

Updated on September 18, 2022

Comments

  • Bembelz
    Bembelz almost 2 years

    is it possible to start a ssh remote session and pass a variable to the remote host, which is usable in the opened session.

    For Example:

    Client: testvar="this is my text" Open ssh session (ssh user@remotehost) and pass $Testvar to the remote Session

    Host: Once the connection is opened I want to be able to use the passed variable to be able to read the variable to get the output "this is my text"

    I need my console session to stay live, which doesnt work in usual ssh remote excecution because a command will be excecuted and the ssh session will be terminated right after. (as far as I know)

    Thank you!

    • MariusMatutiae
      MariusMatutiae about 8 years
      you can easily pass a variable, ssh me@remotepc export testvar="some stuff". But what is the use you intend to make of this? Keeping the connection open? Doesn't the usual ssh me@remote work for you? Why not? If this is your problem, there are better solutions.
    • Bembelz
      Bembelz about 8 years
      I´m opening a connection by some kind of script where each connection has it´s own ID, which i want to pass the the remote host. Once the connection is opened I want to send an answer back the the connecting client to let it know that the connection was successfully established. As written below it works in the way described by David and which is basically the same you wrote. Thank you!
  • Bembelz
    Bembelz about 8 years
    Works perfectly, didnt think it was that easy.
  • AlphaBetaGamma
    AlphaBetaGamma over 6 years
    can we pass a variable like $var , where var is known at host