I am trying to take a db backup with a php file that is running from a cronjob. It was running fine when I tested with sample db. But when I used the actual db there was an error.

I am using shell_exec() to run the command from the php file and the error is:

sh: -c: line 0: syntax error near unexpected token `)'

I understand that this happens because the password starts with a special character ')'.

What can I do to solve this issue WITHOUT CHANGING the password?


After wasting some time I got it working. I just had to escape the character like this:

$pass = '\)d@340kgfj';
  • Or use quotes around $pass where it's used. – wurtel Mar 3 '15 at 9:55
  • Tried that, did not work. – Jubayer Arefin Mar 3 '15 at 10:33
  • Double quotes ", right? – wurtel Mar 3 '15 at 11:56
  • I used double quotes for the whole command string like "mysqldump -u{$user} -p{$pass} dbname" – Jubayer Arefin Mar 4 '15 at 4:17
  • Yeah, that's wrong. You should do mysqdump -u"$user" -p"$pass" ... so that any special chars in e.g. $pass are still quoted when the command is parsed. But why enclode the whole thing? Are you passing it to something else? If you do need that, then you can enclose the whole thing with double quotes but then put single quotes around the variables. – wurtel Mar 4 '15 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.