Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to bash command in node exec function?

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)

like image 606
kraag22 Avatar asked Oct 30 '22 13:10

kraag22


1 Answers

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

like image 84
thib3113 Avatar answered Nov 15 '22 06:11

thib3113