how to redirect output of echo over ssh to a file

2020-03-06 02:08发布

问题:

I'm trying to redirect content of a variable to a file through ssh. like:

ssh $MachineIP  echo $CM_Config > $mName/CM_CONFIG

where $CM_Config is a local variable in my host containing multiple line, and $mName/CM_CONFIG is located in $MachineIP how should I redirect local variable to the remote file assuming my ssh configurations are correct. Thanks in advance

回答1:

You must quote the whole command with double quotes

ssh $MachineIP "echo $CM_Config > $mName/CM_CONFIG"

This allows the variables to be replaced by the local shell and the redirection done at the remote host.



回答2:

In my case the problem solved with this command:

ssh $MachineIP " echo \"$CM_Config\" > \"$mName/CM_CONFIG\" "

In fact without \" enclosing my variable, my problem didn't solved. Maybe it is because that the content of these variables are somehow like bash command and are in multiple lines.



标签: linux bash ssh