1

I'm using a bash shell and trying to figure out the right way to write the following:

ssh mysuer@remotehost 'echo "update user set url = \'localhost\' where url = \'mydomain.com\';" >> /tmp/db.sql'

So far the above is not working.

After hitting enter the following line is a > as if it is expecting for me to close an open quote somewhere. What do I need to do so that this

update user set url = 'localhost' where url = 'mydomain.com';

is output to the remote file?

Dave
  • 25
  • 3
  • possible duplicate of [Escaping apostrophe in single quotes string in bash](http://superuser.com/questions/481797/escaping-apostrophe-in-single-quotes-string-in-bash) – kenorb Mar 16 '15 at 21:38

2 Answers2

1

Single quotes can't be escaped, but you can end the existing one, print apostrophe (\') and open the new one.

Here is the right syntax:

ssh user@host 'echo "update user set url = '\''localhost'\'' where url = '\''mydomain.com'\'';" >> /tmp/db.sql'
kenorb
  • 24,736
  • 27
  • 129
  • 199
0

It should work if you remove the backslashes from the single quotes inside the mysql statement:

ssh mysuer@remotehost 'echo "update user set url = 'localhost' where url = 'mydomain.com';" >> /tmp/db.sql'
gogoud
  • 1,316
  • 1
  • 8
  • 12
  • That doesn't work. Instead, what is otuput to the remote file is "update user set url = localhost where url = mydomain.com;". Notice the single quotes are missing around the keywords where I want them. – Dave Mar 16 '15 at 21:31
  • ok, try changing the single quotes inside the mysql statement to escaped double quotes. – gogoud Mar 16 '15 at 21:39
  • here's another way: `ssh mysuer@remotehost 'echo "update user set url = '"'localhost'"' where url = '"'mydomain.com'"';"'` – gogoud Mar 16 '15 at 21:48