Remote for-loop over SSH

Your local shell interpretes the ${i} within double quotes ("), so the command works out to

ssh [email protected] "for i in /tmp/foo* ; do echo; done"

Simply use single quotes (') instead and your problem will disappear:

ssh user@${server} 'for i in /tmp/foo* ; do echo $i; done'

Just ran into this problem a bit back, and the solution given, while it does work is not too effective if you're also pulling in variables from the local shell, prior to the ssh you create an array to iterate over. A bit messier somewhat would be to just escape the $ initially so it'd be

"for i in /tmp/foo* ; do echo \${i}; done"

Which would escape it within the local construct, not the called ssh shell.

Tags:

Bash

Ssh

Quoting