6

I want to run bash script with params in node js child_process.exec()

In doc is String The command to run, with space-separated arguments, but

child.exec("cat path_to_file1 path_to_file2") 

doesn't work. It is internally changed to

/bin/sh -c cat path_to_file1 path_to_file2

which fails. How can I run this correctly? In shell I would fix it this way

/bin/sh -c 'cat path_to_file1 path_to_file2'

(I didn't write callbacks, because I ask only about command param)

1
  • Not sure I understand your question. exec("cat file1 file2") works correctly. Commented Dec 17, 2015 at 15:06

2 Answers 2

3

Use the shell option of exec :

child.exec("cat path_to_file1 path_to_file2", {
 shell : '/bin/bash' 
})

see options : https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but can be updated . So, you force the shell with the shell option .
-1

Did you tried something like this?

var exec = require('child_process').exec;
var child;
child = exec("cat '/route/to/file_a' '/route/to/file_b'", function (error, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    if (error !== null) {
        console.log('exec error: ' + error);
    }
});

That way you can find what is wrong or get the correct output. ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.