Skip to content
Myr

scripts.md

This file

Path

docs/scripts.md

715 lines · 11.4 KiB · Markdown

Last changed by

Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf

History of this file

Take it

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 1–8why, one step back
1# Node.js scripts
23With Execa, you can write scripts with Node.js instead of a shell language. It is [secure](#escaping), [performant](#performance), [simple](#simplicity) and [cross-platform](#shell).45```js6import {$} from 'execa';78const {stdout: name} = await $`cat package.json`

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 10–48why, one step back
10console.log(name);
1112const branch = await $`git branch --show-current`;13await $`dep deploy --branch=${branch}`;1415await Promise.all([16 $`sleep 1`,17 $`sleep 2`,18 $`sleep 3`,19]);2021const dirName = 'foo bar';22await $`mkdir /tmp/${dirName}`;23```2425## Summary2627This file describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).2829### Flexibility3031Unlike 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).3233### Shell3435The 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.3637This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.3839Also, there is no shell syntax to remember: everything is just plain JavaScript.4041If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.4243### Simplicity4445Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).4647[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.48

Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf

line 49why, one step back
49Execa 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.

Fix script examples (#575)amammadbefore adoption0d038e0

lines 50–123why, one step back
50
51### Modularity5253zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.5455Execa 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.5657### Performance5859Spawning a shell for every command comes at a performance cost, which Execa avoids.6061Also, [local binaries](#local-binaries) can be directly executed without using `npx`.6263### Debugging6465Child processes can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).6667Also, Execa's error messages and [properties](#errors) are very detailed to make it clear to determine why a process failed.6869Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.7071## Details7273### Main binary7475```sh76# Bash77bash file.sh78```7980```js81// zx82zx file.js8384// or a shebang can be used:85// #!/usr/bin/env zx86```8788```js89// Execa scripts are just regular Node.js files90node file.js91```9293### Global variables9495```js96// zx97await $`echo example`;98```99100```js101// Execa102import {$} from 'execa';103104await $`echo example`;105```106107### Command execution108109```sh110# Bash111echo example112```113114```js115// zx116await $`echo example`;117```118119```js120// Execa121await $`echo example`;122```123

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

lines 124–147why, one step back
124### Multiline commands
125126```sh127# Bash128npm run build \129 --example-flag-one \130 --example-flag-two131```132133```js134// zx135await $`npm run build ${[136 '--example-flag-one',137 '--example-flag-two',138]}`;139```140141```js142// Execa143await $`npm run build144 --example-flag-one145 --example-flag-two`146```147

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 149–151why, one step back
149
150```sh151# Bash

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

lines 152–153why, one step back
152tmpDir="/tmp"
153mkdir "$tmpDir/filename"

Fix script examples (#575)amammadbefore adoption0d038e0

lines 154–157why, one step back
154```
155156```js157// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 160–163why, one step back
160```
161162```js163// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 166–167why, one step back
166```
167

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 169–171why, one step back
169
170```sh171# Bash

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

line 172why, one step back
172echo $LANG

Fix script examples (#575)amammadbefore adoption0d038e0

lines 173–176why, one step back
173```
174175```js176// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 178–181why, one step back
178```
179180```js181// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 183–184why, one step back
183```
184

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 186–188why, one step back
186
187```sh188# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

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

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 195–198why, one step back
195```
196197```js198// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 200–201why, one step back
200```
201

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 203–205why, one step back
203
204```sh205# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 207–210why, one step back
207```
208209```js210// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 212–215why, one step back
212```
213214```js215// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 217–218why, one step back
217```
218

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 220–222why, one step back
220
221```sh222# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 224–227why, one step back
224```
225226```js227// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 230–233why, one step back
230```
231232```js233// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 236–237why, one step back
236```
237

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

line 239why, one step back
239

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

lines 240–244why, one step back
240```sh
241# Bash242echo one && echo two243```244

Fix script examples (#575)amammadbefore adoption0d038e0

lines 245–246why, one step back
245```js
246// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 248–251why, one step back
248```
249250```js251// Execa

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

lines 252–254why, one step back
252await $`echo one`;
253await $`echo two`;254```

Fix script examples (#575)amammadbefore adoption0d038e0

line 255why, one step back
255

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

lines 256–261why, one step back
256### Parallel commands
257258```sh259# Bash260echo one &261echo two &

Fix script examples (#575)amammadbefore adoption0d038e0

lines 262–263why, one step back
262```
263

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

lines 264–267why, one step back
264```js
265// zx266await Promise.all([$`echo one`, $`echo two`]);267```

Fix script examples (#575)amammadbefore adoption0d038e0

line 268why, one step back
268

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

lines 269–275why, one step back
269```js
270// Execa271await Promise.all([$`echo one`, $`echo two`]);272```273274### Global/shared options275

Fix script examples (#575)amammadbefore adoption0d038e0

lines 276–277why, one step back
276```sh
277# Bash

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

lines 278–281why, one step back
278options="timeout 5"
279$options echo one280$options echo two281$options echo three

Fix script examples (#575)amammadbefore adoption0d038e0

lines 282–285why, one step back
282```
283284```js285// zx

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

lines 286–289why, one step back
286const timeout = '5s';
287await $`echo one`.timeout(timeout);288await $`echo two`.timeout(timeout);289await $`echo three`.timeout(timeout);

Fix script examples (#575)amammadbefore adoption0d038e0

lines 290–293why, one step back
290```
291292```js293// Execa

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

lines 294–300why, one step back
294import {$ as $_} from 'execa';
295296const $ = $_({timeout: 5000});297298await $`echo one`;299await $`echo two`;300await $`echo three`;

Fix script examples (#575)amammadbefore adoption0d038e0

lines 301–321why, one step back
301```
302303### Environment variables304305```sh306# Bash307EXAMPLE=1 example_command308```309310```js311// zx312$.env.EXAMPLE = '1';313await $`example_command`;314delete $.env.EXAMPLE;315```316317```js318// Execa319await $({env: {EXAMPLE: '1'}})`example_command`;320```321

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 323–325why, one step back
323
324```sh325# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 327–330why, one step back
327```
328329```js330// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 332–335why, one step back
332```
333334```js335// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 337–338why, one step back
337```
338

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

line 340why, one step back
340

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

lines 341–354why, one step back
341```js
342// zx343const content = await stdin();344```345346```js347// Execa348import getStdin from 'get-stdin';349350const content = await getStdin();351```352353### Printing to stdout354

Fix script examples (#575)amammadbefore adoption0d038e0

lines 355–356why, one step back
355```sh
356# Bash

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

line 357why, one step back
357echo example

Fix script examples (#575)amammadbefore adoption0d038e0

lines 358–361why, one step back
358```
359360```js361// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 363–366why, one step back
363```
364365```js366// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 368–369why, one step back
368```
369

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 371–373why, one step back
371
372```sh373# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 375–378why, one step back
375```
376377```js378// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 380–382why, one step back
380```
381382```js

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 385–404why, one step back
385```
386387### Verbose mode388389```sh390# Bash391set -v392echo example393```394395```js396// zx >=8397await $`echo example`.verbose();398399// or:400$.verbose = true;401```402403```js404// Execa

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

lines 405–409why, one step back
405import {$ as $_} from 'execa';
406407const $ = $_({verbose: true});408409await $`echo example`;

Fix script examples (#575)amammadbefore adoption0d038e0

lines 410–417why, one step back
410```
411412Or:413414```415NODE_DEBUG=execa node file.js416```417

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

lines 418–423why, one step back
418Which prints:
419420```421[19:49:00.360] $ echo example422```423

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 425–427why, one step back
425
426```sh427# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 429–432why, one step back
429```
430431```js432// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 434–437why, one step back
434```
435436```js437// Execa

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

lines 438–440why, one step back
438await $`npm run build`
439 .pipe`sort`440 .pipe`head -n2`;

Fix script examples (#575)amammadbefore adoption0d038e0

lines 441–442why, one step back
441```
442

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 444–446why, one step back
444
445```sh446# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 448–451why, one step back
448```
449450```js451// zx

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

lines 452–456why, one step back
452const echo = $`echo example`;
453const cat = $`cat`;454echo.pipe(cat)455echo.stderr.pipe(cat.stdin);456await Promise.all([echo, cat]);

Fix script examples (#575)amammadbefore adoption0d038e0

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

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

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

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 466–468why, one step back
466
467```sh468# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 470–473why, one step back
470```
471472```js473// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 475–478why, one step back
475```
476477```js478// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 480–481why, one step back
480```
481

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 483–485why, one step back
483
484```sh485# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 487–490why, one step back
487```
488489```js490// zx

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

lines 491–493why, one step back
491const cat = $`cat`
492fs.createReadStream('file.txt').pipe(cat.stdin)493await cat

Fix script examples (#575)amammadbefore adoption0d038e0

lines 494–497why, one step back
494```
495496```js497// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 499–553why, one step back
499```
500501### Errors502503```sh504# Bash communicates errors only through the exit code and stderr505timeout 1 sleep 2506echo $?507```508509```js510// zx511const {512 stdout,513 stderr,514 exitCode,515 signal,516} = await $`sleep 2`.timeout('1s');517// file:///home/me/Desktop/node_modules/zx/build/core.js:146518// let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);519// ^520// ProcessOutput [Error]:521// at file:///home/me/Desktop/example.js:2:20522// exit code: null523// signal: SIGTERM524// at ChildProcess.<anonymous> (file:///home/me/Desktop/node_modules/zx/build/core.js:146:26)525// at ChildProcess.emit (node:events:512:28)526// at maybeClose (node:internal/child_process:1098:16)527// at Socket.<anonymous> (node:internal/child_process:456:11)528// at Socket.emit (node:events:512:28)529// at Pipe.<anonymous> (node:net:316:12)530// at Pipe.callbackTrampoline (node:internal/async_hooks:130:17) {531// _code: null,532// _signal: 'SIGTERM',533// _stdout: '',534// _stderr: '',535// _combined: ''536// }537```538539```js540// Execa541const {542 stdout,543 stderr,544 exitCode,545 signal,546 signalDescription,547 originalMessage,548 shortMessage,549 command,550 escapedCommand,551 failed,552 timedOut,553 isCanceled,

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

line 554why, one step back
554 isTerminated,

Fix script examples (#575)amammadbefore adoption0d038e0

lines 555–574why, one step back
555 // and other error-related properties: code, etc.
556} = await $({timeout: 1})`sleep 2`;557// file:///home/me/code/execa/lib/kill.js:60558// reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));559// ^560// Error: Command timed out after 1 milliseconds: sleep 2561// Timed out562// at file:///home/me/Desktop/example.js:2:20563// timedOut: true,564// signal: 'SIGTERM',565// originalMessage: 'Timed out',566// shortMessage: 'Command timed out after 1 milliseconds: sleep 2\nTimed out',567// command: 'sleep 2',568// escapedCommand: 'sleep 2',569// exitCode: undefined,570// signalDescription: 'Termination',571// stdout: '',572// stderr: '',573// failed: true,574// isCanceled: false,

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 576–578why, one step back
576// }
577```578

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

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

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

lines 583–584why, one step back
583false
584echo $?

Fix script examples (#575)amammadbefore adoption0d038e0

lines 585–588why, one step back
585```
586587```js588// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 591–594why, one step back
591```
592593```js594// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 597–598why, one step back
597```
598

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 600–602why, one step back
600
601```sh602# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 604–606why, one step back
604```
605606```js

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 609–612why, one step back
609```
610611```js612// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 614–615why, one step back
614```
615

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 617–619why, one step back
617
618```sh619# Bash

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 621–624why, one step back
621```
622623```js624// zx

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 626–629why, one step back
626```
627628```js629// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

line 632why, one step back
632

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

line 634why, one step back
634

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 636–637why, one step back
636```
637

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 639–641why, one step back
639
640```sh641# Bash

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

line 642why, one step back
642cd project

Fix script examples (#575)amammadbefore adoption0d038e0

lines 643–646why, one step back
643```
644645```js646// zx

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

lines 647–650why, one step back
647cd('project');
648649// or:650$.cwd = 'project';

Fix script examples (#575)amammadbefore adoption0d038e0

lines 651–654why, one step back
651```
652653```js654// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 656–657why, one step back
656```
657

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 659–661why, one step back
659
660```sh661# Bash

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

lines 662–665why, one step back
662pushd project
663pwd664popd665pwd

Fix script examples (#575)amammadbefore adoption0d038e0

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

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

lines 670–675why, one step back
670within(async () => {
671 cd('project');672 await $`pwd`;673});674675await $`pwd`;

Fix script examples (#575)amammadbefore adoption0d038e0

lines 676–679why, one step back
676```
677678```js679// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 682–683why, one step back
682```
683

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 685–687why, one step back
685
686```sh687# Bash

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

line 688why, one step back
688echo one &

Fix script examples (#575)amammadbefore adoption0d038e0

lines 689–691why, one step back
689```
690691```js

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 693–696why, one step back
693```
694695```js696// Execa

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 698–699why, one step back
698```
699

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 701–703why, one step back
701
702```sh703# Bash

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

lines 704–705why, one step back
704echo example &
705echo $!

Fix script examples (#575)amammadbefore adoption0d038e0

lines 706–708why, one step back
706```
707708```js

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

lines 710–712why, one step back
710```
711712```js

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

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

Fix script examples (#575)amammadbefore adoption0d038e0

line 715why, one step back
715```