Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to redirect output of echo over ssh to a file

Tags:

linux

bash

ssh

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

like image 578
Hamid Reza Moradi Avatar asked Dec 12 '22 18:12

Hamid Reza Moradi


2 Answers

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.

like image 88
Olaf Dietsche Avatar answered Dec 21 '22 16:12

Olaf Dietsche


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.

like image 26
Hamid Reza Moradi Avatar answered Dec 21 '22 18:12

Hamid Reza Moradi