Skip to content

Instantly share code, notes, and snippets.

@shrinktofit
Last active September 28, 2020 07:18
Show Gist options
  • Save shrinktofit/aa64ba666dc8b5b6d2ac57f6160d5241 to your computer and use it in GitHub Desktop.
Save shrinktofit/aa64ba666dc8b5b6d2ac57f6160d5241 to your computer and use it in GitHub Desktop.
关于 Node.JS spawn() 的 shell 选项

spawn(command, args, options) 时, 若 options 指定了 shell: true ,将用 shell 来启动目标进程; 否则,(在 Windows 上)将直接调用 command 来创建进程。

一个重要的差别就是 shell 会处理执行参数 args,例如,(在 Windows 上)解释引号:

spawn(command, ['"<含有空格的路径>"'], { shell: true })

可执行程序 command 将会获得参数 <含有空格的路径>。因为 shell 会将其中的双引号去除然后传递给 command

反之,若未指定 shell,参数将原封不动地传递给 command,包括双引号。很可能 command 是没有处理带引号的情况的。

另外,在 Windows 上,若未指定用 shell 启动,command 是不能包含引号的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment