How do I escape a string for a shell command in nodejs (V8 Javascript engine)?

Posted by Maciek on Stack Overflow See other posts from Stack Overflow or by Maciek
Published on 2009-11-22T20:21:52Z Indexed on 2010/04/27 23:43 UTC
Read the original article Hit count: 187

In nodejs, the only way to execute external commands is via sys.exec(cmd). I'd like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a command and then push data to it (only to exec and receive its standard+error outputs), so it appears the only way I've got to do this right now is via a single string command such as:

var dangerStr = "bad stuff here";
sys.exec("echo '" + dangerStr + "' | somecommand");

Most answers to questions like this have focused on either regex which doesn't work for me in nodejs (which uses Google's V8 Javascript engine) or native features from other languages like Python.

I'd like to escape dangerStr so that it's safe to compose an exec string like the one above. If it helps, dangerStr will contain JSON data.

© Stack Overflow or respective owner

Related posts about shell-scripting

Related posts about node.js