Skip to content
Myr

scripts.md

This file

Path

docs/scripts.md

831 lines · 13.8 KiB · Markdown

Last changed by

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

History of this file

Take it

The walk stopped after 200 commits: lines older than 0b8e297 point at it.

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 1–2why, one step back
1# Node.js scripts
2

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

lines 3–7why, one step back
3With Execa, you can write scripts with Node.js instead of a shell language. [Compared to Bash and zx](#differences-with-bash-and-zx), this is more:
4 - [performant](#performance)5 - [cross-platform](#shell): [no shell](../readme.md#shell-syntax) is used, only JavaScript.6 - [secure](#escaping): no shell injection.7 - [simple](#simplicity): minimalistic API, no [globals](#global-variables), no [binary](#main-binary), no [builtin CLI utilities](#builtin-utilities).

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 8why, one step back
8 - [featureful](#simplicity): all Execa features are available ([subprocess piping](#piping-stdout-to-another-command), [IPC](#ipc), [transforms](#transforms), [background subprocesses](#background-subprocesses), [cancellation](#cancellation), [local binaries](#local-binaries), [cleanup on exit](../readme.md#cleanup), [interleaved output](#interleaved-output), [forceful termination](../readme.md#forcekillafterdelay), etc.).

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

line 9why, one step back
9 - [easy to debug](#debugging): [verbose mode](#verbose-mode), [detailed errors](#errors), [messages and stack traces](#cancellation), stateless API.

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 10–14why, one step back
10
11```js12import {$} from 'execa';1314const {stdout: name} = await $`cat package.json`

`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562

line 15why, one step back
15 .pipe`grep name`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 16–30why, one step back
16console.log(name);
1718const branch = await $`git branch --show-current`;19await $`dep deploy --branch=${branch}`;2021await Promise.all([22 $`sleep 1`,23 $`sleep 2`,24 $`sleep 3`,25]);2627const dirName = 'foo bar';28await $`mkdir /tmp/${dirName}`;29```30

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

line 31why, one step back
31## Examples

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 32–83why, one step back
32
33### Main binary3435```sh36# Bash37bash file.sh38```3940```js41// zx42zx file.js4344// or a shebang can be used:45// #!/usr/bin/env zx46```4748```js49// Execa scripts are just regular Node.js files50node file.js51```5253### Global variables5455```js56// zx57await $`echo example`;58```5960```js61// Execa62import {$} from 'execa';6364await $`echo example`;65```6667### Command execution6869```sh70# Bash71echo example72```7374```js75// zx76await $`echo example`;77```7879```js80// Execa81await $`echo example`;82```83

Allow newlines in `$` (#843)ehmickybefore adoption1315486

lines 84–107why, one step back
84### Multiline commands
8586```sh87# Bash88npm run build \89 --example-flag-one \90 --example-flag-two91```9293```js94// zx95await $`npm run build ${[96 '--example-flag-one',97 '--example-flag-two',98]}`;99```100101```js102// Execa103await $`npm run build104 --example-flag-one105 --example-flag-two`106```107

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 108why, one step back
108### Concatenation

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 109–111why, one step back
109
110```sh111# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 112–113why, one step back
112tmpDir="/tmp"
113mkdir "$tmpDir/filename"

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 114–117why, one step back
114```
115116```js117// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 118–119why, one step back
118const tmpDir = '/tmp'
119await $`mkdir ${tmpDir}/filename`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 120–123why, one step back
120```
121122```js123// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 124–125why, one step back
124const tmpDir = '/tmp'
125await $`mkdir ${tmpDir}/filename`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 126–127why, one step back
126```
127

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 128why, one step back
128### Variable substitution

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 129–131why, one step back
129
130```sh131# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 132why, one step back
132echo $LANG

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 133–136why, one step back
133```
134135```js136// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 137why, one step back
137await $`echo $LANG`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 138–141why, one step back
138```
139140```js141// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 142why, one step back
142await $`echo ${process.env.LANG}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 143–144why, one step back
143```
144

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 145why, one step back
145### Escaping

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 146–148why, one step back
146
147```sh148# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 149why, one step back
149echo 'one two'

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 150–153why, one step back
150```
151152```js153// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 154why, one step back
154await $`echo ${'one two'}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 155–158why, one step back
155```
156157```js158// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 159why, one step back
159await $`echo ${'one two'}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 160–161why, one step back
160```
161

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 162why, one step back
162### Escaping multiple arguments

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 163–165why, one step back
163
164```sh165# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 166why, one step back
166echo 'one two' '$'

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 167–170why, one step back
167```
168169```js170// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 171why, one step back
171await $`echo ${['one two', '$']}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 172–175why, one step back
172```
173174```js175// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 176why, one step back
176await $`echo ${['one two', '$']}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 177–178why, one step back
177```
178

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 179why, one step back
179### Subcommands

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 180–182why, one step back
180
181```sh182# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 183why, one step back
183echo "$(echo example)"

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 184–187why, one step back
184```
185186```js187// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 188–189why, one step back
188const example = await $`echo example`;
189await $`echo ${example}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 190–193why, one step back
190```
191192```js193// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 194–195why, one step back
194const example = await $`echo example`;
195await $`echo ${example}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 196–197why, one step back
196```
197

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 198why, one step back
198### Serial commands

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 199why, one step back
199

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 200–204why, one step back
200```sh
201# Bash202echo one && echo two203```204

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 205–206why, one step back
205```js
206// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 207why, one step back
207await $`echo one && echo two`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 208–211why, one step back
208```
209210```js211// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 212–214why, one step back
212await $`echo one`;
213await $`echo two`;214```

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 215why, one step back
215

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 216–221why, one step back
216### Parallel commands
217218```sh219# Bash220echo one &221echo two &

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 222–223why, one step back
222```
223

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 224–227why, one step back
224```js
225// zx226await Promise.all([$`echo one`, $`echo two`]);227```

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 228why, one step back
228

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 229–235why, one step back
229```js
230// Execa231await Promise.all([$`echo one`, $`echo two`]);232```233234### Global/shared options235

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 236–237why, one step back
236```sh
237# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 238–241why, one step back
238options="timeout 5"
239$options echo one240$options echo two241$options echo three

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 242–245why, one step back
242```
243244```js245// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 246–249why, one step back
246const timeout = '5s';
247await $`echo one`.timeout(timeout);248await $`echo two`.timeout(timeout);249await $`echo three`.timeout(timeout);

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 250–253why, one step back
250```
251252```js253// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 254–260why, one step back
254import {$ as $_} from 'execa';
255256const $ = $_({timeout: 5000});257258await $`echo one`;259await $`echo two`;260await $`echo three`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 261–281why, one step back
261```
262263### Environment variables264265```sh266# Bash267EXAMPLE=1 example_command268```269270```js271// zx272$.env.EXAMPLE = '1';273await $`example_command`;274delete $.env.EXAMPLE;275```276277```js278// Execa279await $({env: {EXAMPLE: '1'}})`example_command`;280```281

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 282why, one step back
282### Local binaries

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 283–285why, one step back
283
284```sh285# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 286why, one step back
286npx tsc --version

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 287–290why, one step back
287```
288289```js290// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 291why, one step back
291await $`npx tsc --version`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 292–295why, one step back
292```
293294```js295// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 296why, one step back
296await $`tsc --version`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 297–298why, one step back
297```
298

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 299why, one step back
299### Builtin utilities

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 300why, one step back
300

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 301–314why, one step back
301```js
302// zx303const content = await stdin();304```305306```js307// Execa308import getStdin from 'get-stdin';309310const content = await getStdin();311```312313### Printing to stdout314

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 315–316why, one step back
315```sh
316# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 317why, one step back
317echo example

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 318–321why, one step back
318```
319320```js321// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 322why, one step back
322echo`example`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 323–326why, one step back
323```
324325```js326// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 327why, one step back
327console.log('example');

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 328–329why, one step back
328```
329

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 330why, one step back
330### Silent stderr

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 331–333why, one step back
331
332```sh333# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 334why, one step back
334echo example 2> /dev/null

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 335–338why, one step back
335```
336337```js338// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 339why, one step back
339await $`echo example`.stdio('inherit', 'pipe', 'ignore');

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 340–342why, one step back
340```
341342```js

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 343–344why, one step back
343// Execa does not print stdout/stderr by default
344await $`echo example`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 345–364why, one step back
345```
346347### Verbose mode348349```sh350# Bash351set -v352echo example353```354355```js356// zx >=8357await $`echo example`.verbose();358359// or:360$.verbose = true;361```362363```js364// Execa

Improve documentation of global script options (#870)ehmickybefore adoption146fa07

lines 365–366why, one step back
365import {$ as $_} from 'execa';
366

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

lines 367–368why, one step back
367// `verbose: 'short'` is also available
368const $ = $_({verbose: 'full'});

Improve documentation of global script options (#870)ehmickybefore adoption146fa07

lines 369–370why, one step back
369
370await $`echo example`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 371–378why, one step back
371```
372373Or:374375```376NODE_DEBUG=execa node file.js377```378

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

lines 379–381why, one step back
379Which prints:
380381```

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

lines 382–383why, one step back
382[19:49:00.360] [0] $ echo example
383example

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

line 384why, one step back
384[19:49:00.383] [0] √ (done in 23ms)

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

lines 385–386why, one step back
385```
386

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 387why, one step back
387### Piping stdout to another command

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 388–390why, one step back
388
389```sh390# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 391why, one step back
391echo npm run build | sort | head -n2

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 392–395why, one step back
392```
393394```js395// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 396why, one step back
396await $`npm run build | sort | head -n2`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 397–400why, one step back
397```
398399```js400// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 401–403why, one step back
401await $`npm run build`
402 .pipe`sort`403 .pipe`head -n2`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 404–405why, one step back
404```
405

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 406why, one step back
406### Piping stdout and stderr to another command

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 407–409why, one step back
407
408```sh409# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 410why, one step back
410echo example |& cat

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 411–414why, one step back
411```
412413```js414// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 415–419why, one step back
415const echo = $`echo example`;
416const cat = $`cat`;417echo.pipe(cat)418echo.stderr.pipe(cat.stdin);419await Promise.all([echo, cat]);

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 420–423why, one step back
420```
421422```js423// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 424–425why, one step back
424await $({all: true})`echo example`
425 .pipe({from: 'all'})`cat`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 426–427why, one step back
426```
427

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 428why, one step back
428### Piping stdout to a file

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 429–431why, one step back
429
430```sh431# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 432why, one step back
432echo example > file.txt

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 433–436why, one step back
433```
434435```js436// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 437why, one step back
437await $`echo example`.pipe(fs.createWriteStream('file.txt'));

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 438–441why, one step back
438```
439440```js441// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 442why, one step back
442await $({stdout: {file: 'file.txt'}})`echo example`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 443–444why, one step back
443```
444

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 445why, one step back
445### Piping stdin from a file

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 446–448why, one step back
446
447```sh448# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 449why, one step back
449echo example < file.txt

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 450–453why, one step back
450```
451452```js453// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 454–456why, one step back
454const cat = $`cat`
455fs.createReadStream('file.txt').pipe(cat.stdin)456await cat

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 457–460why, one step back
457```
458459```js460// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 461why, one step back
461await $({inputFile: 'file.txt'})`cat`

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 462–463why, one step back
462```
463

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 464–490why, one step back
464### Iterate over output lines
465466```sh467# Bash468while read469do470 if [[ "$REPLY" == *ERROR* ]]471 then472 echo "$REPLY"473 fi474done < <(npm run build)475```476477```js478// zx does not allow proper iteration.479// For example, the iteration does not handle subprocess errors.480```481482```js483// Execa484for await (const line of $`npm run build`) {485 if (line.includes('ERROR')) {486 console.log(line);487 }488}489```490

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 491–546why, one step back
491### Errors
492493```sh494# Bash communicates errors only through the exit code and stderr495timeout 1 sleep 2496echo $?497```498499```js500// zx501const {502 stdout,503 stderr,504 exitCode,505 signal,506} = await $`sleep 2`.timeout('1s');507// file:///home/me/Desktop/node_modules/zx/build/core.js:146508// let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);509// ^510// ProcessOutput [Error]:511// at file:///home/me/Desktop/example.js:2:20512// exit code: null513// signal: SIGTERM514// at ChildProcess.<anonymous> (file:///home/me/Desktop/node_modules/zx/build/core.js:146:26)515// at ChildProcess.emit (node:events:512:28)516// at maybeClose (node:internal/child_process:1098:16)517// at Socket.<anonymous> (node:internal/child_process:456:11)518// at Socket.emit (node:events:512:28)519// at Pipe.<anonymous> (node:net:316:12)520// at Pipe.callbackTrampoline (node:internal/async_hooks:130:17) {521// _code: null,522// _signal: 'SIGTERM',523// _stdout: '',524// _stderr: '',525// _combined: ''526// }527```528529```js530// Execa531const {532 stdout,533 stderr,534 exitCode,535 signal,536 signalDescription,537 originalMessage,538 shortMessage,539 command,540 escapedCommand,541 failed,542 timedOut,543 isCanceled,544 isTerminated,545 // and other error-related properties: code, etc.546} = await $({timeout: 1})`sleep 2`;

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 547why, one step back
547// ExecaError: Command timed out after 1 milliseconds: sleep 2

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 548why, one step back
548// at file:///home/me/Desktop/example.js:2:20

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 549why, one step back
549// at ... {

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 550why, one step back
550// shortMessage: 'Command timed out after 1 milliseconds: sleep 2\nTimed out',

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 551why, one step back
551// originalMessage: '',

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 552–553why, one step back
552// command: 'sleep 2',
553// escapedCommand: 'sleep 2',

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 554–560why, one step back
554// cwd: '/path/to/cwd',
555// durationMs: 19.95693,556// failed: true,557// timedOut: true,558// isCanceled: false,559// isTerminated: true,560// signal: 'SIGTERM',

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 561–563why, one step back
561// signalDescription: 'Termination',
562// stdout: '',563// stderr: '',

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 564–565why, one step back
564// stdio: [undefined, '', ''],
565// pipedFrom: []

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 566–568why, one step back
566// }
567```568

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 569why, one step back
569### Exit codes

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 570–572why, one step back
570
571```sh572# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 573–574why, one step back
573false
574echo $?

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 575–578why, one step back
575```
576577```js578// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 579–580why, one step back
579const {exitCode} = await $`false`.nothrow();
580echo`${exitCode}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 581–584why, one step back
581```
582583```js584// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 585–586why, one step back
585const {exitCode} = await $({reject: false})`false`;
586console.log(exitCode);

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 587–588why, one step back
587```
588

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 589why, one step back
589### Timeouts

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 590–592why, one step back
590
591```sh592# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 593why, one step back
593timeout 5 echo example

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 594–596why, one step back
594```
595596```js

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 597–598why, one step back
597// zx
598await $`echo example`.timeout('5s');

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 599–602why, one step back
599```
600601```js602// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 603why, one step back
603await $({timeout: 5000})`echo example`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 604–605why, one step back
604```
605

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 606why, one step back
606### Current filename

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 607–609why, one step back
607
608```sh609# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 610why, one step back
610echo "$(basename "$0")"

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 611–614why, one step back
611```
612613```js614// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 615why, one step back
615await $`echo ${__filename}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 616–619why, one step back
616```
617618```js619// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 620–621why, one step back
620import {fileURLToPath} from 'node:url';
621import path from 'node:path';

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 622why, one step back
622

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 623why, one step back
623const __filename = path.basename(fileURLToPath(import.meta.url));

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 624why, one step back
624

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 625why, one step back
625await $`echo ${__filename}`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 626–627why, one step back
626```
627

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 628why, one step back
628### Current directory

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 629–631why, one step back
629
630```sh631# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 632why, one step back
632cd project

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 633–636why, one step back
633```
634635```js636// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 637–640why, one step back
637cd('project');
638639// or:640$.cwd = 'project';

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 641–644why, one step back
641```
642643```js644// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 645why, one step back
645const $$ = $({cwd: 'project'});

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 646–647why, one step back
646```
647

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 648why, one step back
648### Multiple current directories

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 649–651why, one step back
649
650```sh651# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 652–655why, one step back
652pushd project
653pwd654popd655pwd

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 656–659why, one step back
656```
657658```js659// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 660–665why, one step back
660within(async () => {
661 cd('project');662 await $`pwd`;663});664665await $`pwd`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 666–669why, one step back
666```
667668```js669// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 670–671why, one step back
670await $({cwd: 'project'})`pwd`;
671await $`pwd`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 672–673why, one step back
672```
673

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 674why, one step back
674### Background subprocesses

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 675–677why, one step back
675
676```sh677# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 678why, one step back
678echo one &

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 679–681why, one step back
679```
680681```js

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 682why, one step back
682// zx does not allow setting the `detached` option

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 683–686why, one step back
683```
684685```js686// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 687why, one step back
687await $({detached: true})`echo one`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 688–689why, one step back
688```
689

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

lines 690–701why, one step back
690### IPC
691692```sh693# Bash does not allow simple IPC694```695696```js697// zx does not allow simple IPC698```699700```js701// Execa

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 702why, one step back
702const subprocess = $({ipc: true})`node script.js`;

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

line 703why, one step back
703

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 704why, one step back
704subprocess.on('message', message => {

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

line 705why, one step back
705 if (message === 'ping') {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 706why, one step back
706 subprocess.send('pong');

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

lines 707–740why, one step back
707 }
708});709```710711### Transforms712713```sh714# Bash does not allow transforms715```716717```js718// zx does not allow transforms719```720721```js722// Execa723const transform = function * (line) {724 if (!line.includes('secret')) {725 yield line;726 }727};728729await $({stdout: [transform, 'inherit']})`echo ${'This is a secret.'}`;730```731732### Cancellation733734```sh735# Bash736kill $PID737```738739```js740// zx

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 741why, one step back
741subprocess.kill();

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

lines 742–746why, one step back
742```
743744```js745// Execa746// Can specify an error message and stack trace

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 747why, one step back
747subprocess.kill(error);

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

lines 748–769why, one step back
748
749// Or use an `AbortSignal`750const controller = new AbortController();751await $({signal: controller.signal})`node long-script.js`;752```753754### Interleaved output755756```sh757# Bash prints stdout and stderr interleaved758```759760```js761// zx separates stdout and stderr762const {stdout, stderr} = await $`node example.js`;763```764765```js766// Execa can interleave stdout and stderr767const {all} = await $({all: true})`node example.js`;768```769

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 770why, one step back
770### PID

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 771–773why, one step back
771
772```sh773# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 774–775why, one step back
774echo example &
775echo $!

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 776–778why, one step back
776```
777778```js

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 779why, one step back
779// zx does not return `subprocess.pid`

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 780–782why, one step back
780```
781782```js

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 783–784why, one step back
783// Execa
784const {pid} = $`echo example`;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 785why, one step back
785```

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

lines 786–810why, one step back
786
787## Differences with Bash and zx788789This section describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).790791### Flexibility792793Unlike shell languages like Bash, libraries like Execa and zx enable you to write scripts with a more featureful programming language (JavaScript). This allows complex logic (such as [parallel execution](#parallel-commands)) to be expressed easily. This also lets you use [any Node.js package](#builtin-utilities).794795### Shell796797The main difference between Execa and zx is that Execa does not require any shell. Shell-specific keywords and features are [written in JavaScript](#variable-substitution) instead.798799This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.800801Also, there is no shell syntax to remember: everything is just plain JavaScript.802803If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.804805### Simplicity806807Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).808809[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.810

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 811why, one step back
811Execa is a thin wrapper around the core Node.js [`child_process` module](https://nodejs.org/api/child_process.html). Unlike zx, it lets you use [any of its native features](#background-subprocesses): [`pid`](#pid), [IPC](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback), [`unref()`](https://nodejs.org/api/child_process.html#subprocessunref), [`detached`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`uid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`gid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`cancelSignal`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), etc.

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

lines 812–826why, one step back
812
813### Modularity814815zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.816817Execa does not include [any utility](#builtin-utilities): it focuses on being small and modular instead. Any Node.js package can be used in your scripts.818819### Performance820821Spawning a shell for every command comes at a performance cost, which Execa avoids.822823Also, [local binaries](#local-binaries) can be directly executed without using `npx`.824825### Debugging826

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 827why, one step back
827Subprocesses can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

line 828why, one step back
828

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 829why, one step back
829Also, Execa's error messages and [properties](#errors) are very detailed to make it clear to determine why a subprocess failed. Error messages and stack traces can be set with [`subprocess.kill(error)`](../readme.md#killerror).

Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598

lines 830–831why, one step back
830
831Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.