如何在node exec函数中将参数传递给bash命令?

kra*_*g22 5 child-process node.js electron

我想在节点 js 中使用参数运行 bash 脚本 child_process.exec()

在文档中是String The command to run, with space-separated arguments,但是

child.exec("cat path_to_file1 path_to_file2") 
Run Code Online (Sandbox Code Playgroud)

不起作用。它在内部更改为

/bin/sh -c cat path_to_file1 path_to_file2
Run Code Online (Sandbox Code Playgroud)

这失败了。我怎样才能正确运行它?在 shell 中,我会以这种方式修复它

/bin/sh -c 'cat path_to_file1 path_to_file2'
Run Code Online (Sandbox Code Playgroud)

(我没有写回调,因为我只询问命令参数)

thi*_*113 3

使用 exec 的 shell 选项:

child.exec("cat path_to_file1 path_to_file2", {
 shell : '/bin/bash' 
})
Run Code Online (Sandbox Code Playgroud)

查看选项:https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback