Reputation: 3016
I'm trying to remotely process a while read line loop via ssh with the following:
ssh /root/.ssh/id_rsa "while read var; do echo \"- $var -\"; done < /tmp/file; cat /tmp/file"
/tmp/file exists remotely. Output of the command should be:
- /tmp -
- /var -
/tmp
/var
But output actually is:
- -
- -
/tmp
/var
Why doesn't $var get filled here when remotely executed ? If I happen to connect a console via ssh to the machine and execute the same command, it works as expected.
Upvotes: 1
Views: 550
Reputation: 14520
$var
is getting expanded before the ssh
because it's inside double quotes. If you make the command you're passing to ssh
be in single quotes it won't get expanded, or if you escape the $
you should be set:
ssh /root/.ssh/id_rsa 'while read var; do echo "- $var -"; done < /tmp/file; cat /tmp/file'
or
ssh /root/.ssh/id_rsa "while read var; do echo \"- \$var -\"; done < /tmp/file; cat /tmp/file"
Upvotes: 1
Reputation: 4034
You should use single quotes or escape the $
in $var
. Right now $var
is getting substituted before ssh runs.
ssh /root/.ssh/id_rsa 'while read var; do echo "- $var -"; done < /tmp/file; cat /tmp/file'
Upvotes: 2