4

Is there a way to have environment variables on a remote host evaluated when using SCP?

For instance, let's say I have $FOO= /mydir on host A; how would I do something like:

scp dogbert@hostA:$FOO/evil-secret-plans.txt .

Of course, $FOO isn't defined on my local machine so the above command doesn't work as it is.

3 Answers 3

1

Try it like this:

scp dogbert@hostA:$(ssh dagobert@hostA "echo $FOO")/evil-secret-plans.txt .

A bit of overhead.

1
  • shouldn't that be scp dogbert@hostA:$(ssh dagobert@hostA "echo \$FOO")/evil-secret-plans.txt . (escaping \$FOO) ?
    – n00dl3
    Commented Sep 27, 2017 at 8:45
2

Add a \:

scp dogbert@hostA:"\$FOO/evil-secret-plans.txt" .
1

The easier thing to do is to send the file with tar or gzip through stdout / stdin.

tar -czf - ./evilsecretfile.txt | ssh user@remoteHost "cd \${varOnRemoteSystem} ; tar -xzf - ."

or

gzip -c localFileName.txt | ssh user@remoteHost "gzip -d > /remote/path/\${varOnRemoteSystem} ; chmod 666 /remote/path\${varOnRemoteSystem}"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .