Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 1 down vote accepted

Try it like this:

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

A bit of overhead.

share|improve this answer

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}"

share|improve this answer

Add a \:

scp dogbert@hostA:"\$FOO/evil-secret-plans.txt" .
share|improve this answer

You must log in to answer this question.

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