Search code examples
linuxbashshellscriptingsuse

How do I pass a variable from my local server to a remote server?


I am trying to pass a variable from my local server (location1) to a remote server (location2). The purpose of the code is to copy a file from the remote server from a predefined location. In simpler terms, I want to copy files from location2 to location1 using a predefined path where location1 is on the local server and location2 is a remote server. See the code segment:

 $location1=somewhere/on/local_server
 $location2=somewhere/on/remote_server

 sshpass -p "password" ssh [email protected] 'su -lc "cp -r $location2 $location1";'

The error I get is that both $location1 and $location2 are undefined. Also, I DON'T want to manually type the location paths because they could change at any time and it would be a pain to change them in the code if done manually.


Solution

  • You can do:

    sshpass -p "password" ssh [email protected] "su -lc \"cp -r $location2 $location1\""