0

This is a variation of the question here: How have both local and remote variable inside an SSH command

How do I get the "ssh-variable" B to my local computer?

 A=3
 ssh host@name "echo $A; B=5; echo $A; echo \$B;"
 echo $A
 echo $B
 echo \$B

returns 3, NOTHING, $B

How can I locally access a value for B?

some explanations based on comments: I am using a bash script to access a remote host to modify some things there, amongst other things create a workspace there. The location of this workspace is what I want to store in a variable and save for later use.

Basically, I have a function to go to the remote host and make the workspace and then another function to use the path to that workspace to do other things there.

I was hoping for a lightweight, slim solution that can be integrated and easily read in a command similar to this:

 ssh host@name "ws_allocate myworkspace; workspace=ws_find myworkspace;"

and then locally use $workspace. This is part of a larger bash script, that should be easy to understand for non-expert bash users (like myself)...

Turns out this is a duplicate of bash—Better way to store variable between runs?

6
  • You can save the content of the remote variable in a file, and then recover the file content in your localhost. Commented Oct 18, 2018 at 13:21
  • this waaay too complicated for my purpose ;) Commented Oct 18, 2018 at 13:23
  • Why? Write a local script, scp to upload the local script in the remote server, ssh to execute the script and store result in a /tmp file (in the remote server), scp to download the response, and then process the answer locally. Commented Oct 18, 2018 at 13:27
  • Think about it: you want a variable defined by a remote shell to be defined in your local shell. That's some significant interprocess communication; it's not going to be simple. Commented Oct 18, 2018 at 13:27
  • On the other hand, if your example is accurate and the assignment statement is hard-coded in the argument to ssh, why don't you just define it locally to begin with? Commented Oct 18, 2018 at 13:28

2 Answers 2

0

You could do something like this:

X=$(ssh user@host 'echo $X')

This will run echo $X on the server and place the resulting output in a local variable X, which you can later use by saying $X

So to apply this to your example, you could say:

workspace=$(ssh host@name 'ws_allocate myworkspace; ws_find myworkspace;')

Note that the local variable workspace will contain the output from the command that is run remotely. So I'm assuming that in your example only the ws_find myworkspace will print any output and that the call to ws_allocate is silent.

Sign up to request clarification or add additional context in comments.

5 Comments

C=$(ssh user@host "echo $B"); echo $C empty output
This is because you are using double quotes "echo $B". Try using single quotes: 'echo $B'
can it bee that B is lost when ssh disconnects and therefore is not available when I connect again to save it in C with echo?
Yes; if you made the variable in a previous ssh session it will not be available in the next session. But in that case I also don't get the point of doing this, please clarify what you are trying to do.
thanks, I have learnt a lot (again). In my example, this will not work out I am afraid (see my answer below)
-2

Ok, so the solution that appears to me to be the easiest and that allows the value of $B to be stored on the remote host is to save it in a file. As described and discussed here already: bash—Better way to store variable between runs? :(

ssh ${supercomputer} "
ws_list -a >workspaces.txt;
if grep -q "${experiment}" workspaces.txt; 
then echo 'we have a workspace already'; 
else ws_allocate ${experiment} 30; 
fi; 
ws_find ${experiment} > mypathtoworkspace;"

where supercomputer is the address to the host, experiment the workspace I want to create (or not, if it is there already) and workspace the workspace I want to create for my experiment.

I wanted to use the path to the remote workspace locally, e.g.

 echo "${workspace}"

but this seems to not be possible - therefore I will download the file I am writing on the remote host and read the path from that file to use it locally

If anyone sees a better solution it is welcome!

4 Comments

This does not make sense to me. Your question is about getting a remote variable to the local machine, and the answer is to store a local value on the remote machine?
it is a remote value that is stored on the remote machine. I am not leaving my remote host in this ssh command
I see. Do you agree that this is not what the question is about?
in the overall context it is, in the context of the answer maybe not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.