Skip to content

Commit 95eb02d

Browse files
jvelezpoaddaleax
authored andcommitted
doc: shell option for the execFile and execFileSync functions
Useful for executing in a shell because it accepts arguments as an array instead of a string as exec does. Depending on the circumstances, that can prove to be useful if the arguments are already prepared. PR-URL: #18237 Fixes: #18199 Reviewed-By: Julian Duque <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Adrian Estrada <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 27ee083 commit 95eb02d

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

doc/api/child_process.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ implemented on top of [`child_process.spawn()`][] or [`child_process.spawnSync()
4444
* [`child_process.exec()`][]: spawns a shell and runs a command within that shell,
4545
passing the `stdout` and `stderr` to a callback function when complete.
4646
* [`child_process.execFile()`][]: similar to [`child_process.exec()`][] except that
47-
it spawns the command directly without first spawning a shell.
47+
it spawns the command directly without first spawning a shell by default.
4848
* [`child_process.fork()`][]: spawns a new Node.js process and invokes a
4949
specified module with an IPC communication channel established that allows
5050
sending messages between parent and child.
@@ -78,7 +78,7 @@ when the child process terminates.
7878
The importance of the distinction between [`child_process.exec()`][] and
7979
[`child_process.execFile()`][] can vary based on platform. On Unix-type operating
8080
systems (Unix, Linux, macOS) [`child_process.execFile()`][] can be more efficient
81-
because it does not spawn a shell. On Windows, however, `.bat` and `.cmd`
81+
because it does not spawn a shell by default. On Windows, however, `.bat` and `.cmd`
8282
files are not executable on their own without a terminal, and therefore cannot
8383
be launched using [`child_process.execFile()`][]. When running on Windows, `.bat`
8484
and `.cmd` files can be invoked using [`child_process.spawn()`][] with the `shell`
@@ -266,14 +266,18 @@ changes:
266266
normally be created on Windows systems. **Default:** `false`.
267267
* `windowsVerbatimArguments` {boolean} No quoting or escaping of arguments is
268268
done on Windows. Ignored on Unix. **Default:** `false`.
269+
* `shell` {boolean|string} If `true`, runs `command` inside of a shell. Uses
270+
`'/bin/sh'` on UNIX, and `process.env.ComSpec` on Windows. A different
271+
shell can be specified as a string. See [Shell Requirements][] and
272+
[Default Windows Shell][]. **Default:** `false` (no shell).
269273
* `callback` {Function} Called with the output when process terminates.
270274
* `error` {Error}
271275
* `stdout` {string|Buffer}
272276
* `stderr` {string|Buffer}
273277
* Returns: {ChildProcess}
274278

275279
The `child_process.execFile()` function is similar to [`child_process.exec()`][]
276-
except that it does not spawn a shell. Rather, the specified executable `file`
280+
except that it does not spawn a shell by default. Rather, the specified executable `file`
277281
is spawned directly as a new process making it slightly more efficient than
278282
[`child_process.exec()`][].
279283

@@ -312,6 +316,10 @@ async function getVersion() {
312316
getVersion();
313317
```
314318

319+
*Note*: If the `shell` option is enabled, do not pass unsanitized user input
320+
to this function. Any input containing shell metacharacters may be used to
321+
trigger arbitrary command execution.
322+
315323
### child_process.fork(modulePath[, args][, options])
316324
<!-- YAML
317325
added: v0.5.0
@@ -705,6 +713,10 @@ changes:
705713
* `encoding` {string} The encoding used for all stdio inputs and outputs. **Default:** `'buffer'`
706714
* `windowsHide` {boolean} Hide the subprocess console window that would
707715
normally be created on Windows systems. **Default:** `false`.
716+
* `shell` {boolean|string} If `true`, runs `command` inside of a shell. Uses
717+
`'/bin/sh'` on UNIX, and `process.env.ComSpec` on Windows. A different
718+
shell can be specified as a string. See [Shell Requirements][] and
719+
[Default Windows Shell][]. **Default:** `false` (no shell).
708720
* Returns: {Buffer|string} The stdout from the command.
709721

710722
The `child_process.execFileSync()` method is generally identical to
@@ -721,6 +733,10 @@ If the process times out or has a non-zero exit code, this method ***will***
721733
throw an [`Error`][] that will include the full result of the underlying
722734
[`child_process.spawnSync()`][].
723735

736+
*Note*: If the `shell` option is enabled, do not pass unsanitized user input
737+
to this function. Any input containing shell metacharacters may be used to
738+
trigger arbitrary command execution.
739+
724740
### child_process.execSync(command[, options])
725741
<!-- YAML
726742
added: v0.11.12

0 commit comments

Comments
 (0)