Get local variable from ssh call in shell
I have a script called global.sh which copies a shell script called my_local_script.sh on a remote machine.
scp my_local_script.sh host@user_a:remote_directory
Then i connect to that ssh host and execute it like this
ssh host@user " cd remote_directory ./my_local_script.sh "
So far so good and everything is ok, the problem is that i want to get a local variable inside my_local_script.sh to global.sh. I tried doing this
ssh host@user " cd remote_directory my_arg=$(./my_local_script.sh) "
But this does not work and the even the call of my local script does not work properly anymore. Any suggestions how can i overcome this issue.
PS : Copying the variable to a temporary file is not an option. I am looking for a shell solution for this if possible.
You are using a command substitution, which is necessary, but it is in the wrong place. You should place it around the entire SSH command:
my_var=$(ssh host@user "cd remote_directory; ./my_local_script.sh")
This is assuming that the variable you want to capture is being outputted by my_local_script eg by echo or printf.