Search code examples
linuxshellunixsshsshpass

SSHpass : to access local variable on remote host


Unable to access local variable on remote host using sshpass. Can you please help me to pass local variable to pass on remote host using ssh pass for the below code snippet

name="Stack over flow"  
sshpass -p${serverpassword} ssh -o StrictHostKeyChecking=no ${username}@${servername} 'for i in $name ; do echo $i ; done'

expected result:

stack
over
flow

Solution

  • Try this:

    name="Stack over flow"  
    sshpass -p${serverpassword} ssh -o StrictHostKeyChecking=no ${username}@${servername} \
    "for i in \\$name; do echo \$i; done"
    

    You need to double quote your command line and right escaping.