how to redirect output of echo over ssh to a file

2020-03-06 01:36发布

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

标签: linux bash ssh
2条回答
地球回转人心会变
2楼-- · 2020-03-06 02:10

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.

查看更多
太酷不给撩
3楼-- · 2020-03-06 02:13

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.

查看更多
登录 后发表回答