SSH remote execute command with special characters

MySQLremote-accessssh

I have a problem executing a remote command through SSH that has special character in it. I'm trying to export a database from MySQL for back up purposes. This is the command I have in my local script:

#!/bin/bash
ssh user@host "mysqldump -u dbname -p'p$ssw0rd' --lock-tables=false dbname > ~/sites/mysite/backup/dbname.sql"

As you can see I'm using the ' characters around my password. When I execute this script I get the following error:

mysqldump: Got error: 1045: Access denied for user 'dbname'@'localhost' (using password: YES) when trying to connect

However, when I ssh into the machine manually and execute the command it executes just fine. I get the impression that through executing the command through ssh something is lost and the password doesn't come through.

How can I execute my command with special characters through ssh?

Best Answer

You can try escaping the special characters in the command that you are passing to the ssh. In which case your

p$ssw0rd

becomes

p\$ssw0rd

Since the shell you are using might interpret "\" character differently, I can not vouch for 100% solution to your case, but it won't hurt to try.