1

I'm trying to echo multiline variable over ssh, but the second line gets executed as a command.

msg="Hello World; Hello World2"; echo $msg; ssh -q newhost "echo "$msg""

Output:

Hello World; Hello World2

Hello World

bash: Hello: command not found
gaganso
  • 2,914
  • 2
  • 26
  • 43
vortex0220
  • 11
  • 1

1 Answers1

0

Is there a reason it needs to be a multi-line variable? why not send two distinct messages?

    msg="Hello World"; msg2="Hello World2"; echo $msg; ssh -q localhost "echo "$msg""$msg2""

works fine

echo -e will let you interpret escaped characters like \n which you should use instead of semicolon ;

user4922
  • 101
  • 2
  • If you are set on multiline I just found out that echo -e Hello\nWorld May give you what you need. – user4922 Jul 16 '18 at 21:13
  • msg="Hello World\nHello World2"; echo -e $msg; – user4922 Jul 16 '18 at 21:14
  • The example I provided in the topic is a brief simplified version of what I'm trying to do. I actually have a file with number of commands there. I assign content from that file to a variable and trying to echo it on different host over ssh. That is why it should be in one multiline variable. But instead of echoing it all commands from the file get executed. – vortex0220 Jul 17 '18 at 20:12
  • Here is another example: A="hostname;hostname"; echo -e "$A"; ssh -q newhost01 "echo -e $A" hostname;hostname hostname newhost01 – vortex0220 Jul 19 '18 at 14:20
  • can you use the newline character instead of the semi colon? \n instead of ; – user4922 Jul 19 '18 at 14:39
  • A="hostname\nhostname"; echo -e "$A"; ssh localhost "echo -e \"$A\""; – user4922 Jul 26 '18 at 15:18
  • Next time try searching for your answer on here: https://stackoverflow.com/questions/17148688/how-to-display-newline-in-ssh – user4922 Jul 26 '18 at 15:19