Skip to content
Myr

scripts.md

This file

Path

docs/scripts.md

804 lines · 13.5 KiB · Markdown

Last changed by

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

History of this file

Take it

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

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

lines 3–9why, 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).8 - [featureful](#simplicity): all Execa features are available ([process piping](#piping-stdout-to-another-command), [IPC](#ipc), [transforms](#transforms), [background processes](#background-processes), [cancellation](#cancellation), [local binaries](#local-binaries), [cleanup on exit](../readme.md#cleanup), [interleaved output](#interleaved-output), [forceful termination](../readme.md#forcekillafterdelay), etc.).9 - [easy to debug](#debugging): [verbose mode](#verbose-mode), [detailed errors](#errors), [messages and stack traces](#cancellation), stateless API.

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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"

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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}`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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'

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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'}`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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'}`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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' '$'

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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', '$']}`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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', '$']}`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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)"

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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}`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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}`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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```

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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 &

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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```

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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);

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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');

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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');

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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]);

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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'));

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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`

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 462–516why, one step back
462```
463464### Errors465466```sh467# Bash communicates errors only through the exit code and stderr468timeout 1 sleep 2469echo $?470```471472```js473// zx474const {475 stdout,476 stderr,477 exitCode,478 signal,479} = await $`sleep 2`.timeout('1s');480// file:///home/me/Desktop/node_modules/zx/build/core.js:146481// let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);482// ^483// ProcessOutput [Error]:484// at file:///home/me/Desktop/example.js:2:20485// exit code: null486// signal: SIGTERM487// at ChildProcess.<anonymous> (file:///home/me/Desktop/node_modules/zx/build/core.js:146:26)488// at ChildProcess.emit (node:events:512:28)489// at maybeClose (node:internal/child_process:1098:16)490// at Socket.<anonymous> (node:internal/child_process:456:11)491// at Socket.emit (node:events:512:28)492// at Pipe.<anonymous> (node:net:316:12)493// at Pipe.callbackTrampoline (node:internal/async_hooks:130:17) {494// _code: null,495// _signal: 'SIGTERM',496// _stdout: '',497// _stderr: '',498// _combined: ''499// }500```501502```js503// Execa504const {505 stdout,506 stderr,507 exitCode,508 signal,509 signalDescription,510 originalMessage,511 shortMessage,512 command,513 escapedCommand,514 failed,515 timedOut,516 isCanceled,

Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c

line 517why, one step back
517 isTerminated,

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 518–537why, one step back
518 // and other error-related properties: code, etc.
519} = await $({timeout: 1})`sleep 2`;520// file:///home/me/code/execa/lib/kill.js:60521// reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));522// ^523// Error: Command timed out after 1 milliseconds: sleep 2524// Timed out525// at file:///home/me/Desktop/example.js:2:20526// timedOut: true,527// signal: 'SIGTERM',528// originalMessage: 'Timed out',529// shortMessage: 'Command timed out after 1 milliseconds: sleep 2\nTimed out',530// command: 'sleep 2',531// escapedCommand: 'sleep 2',532// exitCode: undefined,533// signalDescription: 'Termination',534// stdout: '',535// stderr: '',536// failed: true,537// isCanceled: false,

Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c

line 538why, one step back
538// isTerminated: false

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 539–541why, one step back
539// }
540```541

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 543–545why, one step back
543
544```sh545# Bash

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

lines 546–547why, one step back
546false
547echo $?

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 548–551why, one step back
548```
549550```js551// zx

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 554–557why, one step back
554```
555556```js557// Execa

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 560–561why, one step back
560```
561

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 563–565why, one step back
563
564```sh565# Bash

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 567–569why, one step back
567```
568569```js

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 572–575why, one step back
572```
573574```js575// Execa

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 577–578why, one step back
577```
578

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 580–582why, one step back
580
581```sh582# Bash

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 584–587why, one step back
584```
585586```js587// zx

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 589–592why, one step back
589```
590591```js592// Execa

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

line 595why, one step back
595

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

line 597why, one step back
597

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 599–600why, one step back
599```
600

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 602–604why, one step back
602
603```sh604# Bash

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

line 605why, one step back
605cd project

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 606–609why, one step back
606```
607608```js609// zx

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

lines 610–613why, one step back
610cd('project');
611612// or:613$.cwd = 'project';

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 614–617why, one step back
614```
615616```js617// Execa

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 619–620why, one step back
619```
620

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 622–624why, one step back
622
623```sh624# Bash

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

lines 625–628why, one step back
625pushd project
626pwd627popd628pwd

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 629–632why, one step back
629```
630631```js632// zx

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

lines 633–638why, one step back
633within(async () => {
634 cd('project');635 await $`pwd`;636});637638await $`pwd`;

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 639–642why, one step back
639```
640641```js642// Execa

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

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

line 647why, one step back
647### Background processes

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 648–650why, one step back
648
649```sh650# Bash

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

line 651why, one step back
651echo one &

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 652–654why, one step back
652```
653654```js

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

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

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 661–662why, one step back
661```
662

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

lines 663–742why, one step back
663### IPC
664665```sh666# Bash does not allow simple IPC667```668669```js670// zx does not allow simple IPC671```672673```js674// Execa675const childProcess = $({ipc: true})`node script.js`;676677childProcess.on('message', message => {678 if (message === 'ping') {679 childProcess.send('pong');680 }681});682```683684### Transforms685686```sh687# Bash does not allow transforms688```689690```js691// zx does not allow transforms692```693694```js695// Execa696const transform = function * (line) {697 if (!line.includes('secret')) {698 yield line;699 }700};701702await $({stdout: [transform, 'inherit']})`echo ${'This is a secret.'}`;703```704705### Cancellation706707```sh708# Bash709kill $PID710```711712```js713// zx714childProcess.kill();715```716717```js718// Execa719// Can specify an error message and stack trace720childProcess.kill(error);721722// Or use an `AbortSignal`723const controller = new AbortController();724await $({signal: controller.signal})`node long-script.js`;725```726727### Interleaved output728729```sh730# Bash prints stdout and stderr interleaved731```732733```js734// zx separates stdout and stderr735const {stdout, stderr} = await $`node example.js`;736```737738```js739// Execa can interleave stdout and stderr740const {all} = await $({all: true})`node example.js`;741```742

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 744–746why, one step back
744
745```sh746# Bash

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

lines 747–748why, one step back
747echo example &
748echo $!

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 749–751why, one step back
749```
750751```js

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

line 752why, one step back
752// zx does not return `childProcess.pid`

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

lines 753–755why, one step back
753```
754755```js

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

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

Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0

line 758why, one step back
758```

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

lines 759–804why, one step back
759
760## Differences with Bash and zx761762This section describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).763764### Flexibility765766Unlike 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).767768### Shell769770The 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.771772This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.773774Also, there is no shell syntax to remember: everything is just plain JavaScript.775776If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.777778### Simplicity779780Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).781782[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.783784Execa 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-processes): [`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.785786### Modularity787788zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.789790Execa 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.791792### Performance793794Spawning a shell for every command comes at a performance cost, which Execa avoids.795796Also, [local binaries](#local-binaries) can be directly executed without using `npx`.797798### Debugging799800Child processes can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).801802Also, Execa's error messages and [properties](#errors) are very detailed to make it clear to determine why a process failed. Error messages and stack traces can be set with [`childProcess.kill(error)`](../readme.md#killerror).803804Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.