javascriptnode.jslinuxescaping

Escape shell string in Node.js


I am trying to run a shell command from my Node.js script:

var child = exec('mysqldump -u "'+dbConfig.user+'" --password="' + dbConfig.password + '" --single-transaction ' + dbConfig.database + ' > ' + dumpFilePath)

Now this is all well and fine however the password can contain special characters that would break the command.

I have been looking into this Node module: https://www.npmjs.com/package/shell-escape

However one of its issues is that it can't parse = sign, which makes the package useless to the above statement.

So my question is how can I safely escape the password field?


Solution

  • You can escape the password safely with single quotes:

    var child = exec('mysqldump -u ' + dbConfig.user + ' --password=\'' + dbConfig.password + '\' --single-transaction ' + dbConfig.database + ' > ' + dumpFilePath)