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 fileTake 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 back1# Node.js scripts23With 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 back9 .pipe`grep name`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 10–48why, one step back10console.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.48Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf
line 49why, one step back49Execa 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 back5051### 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```123Allow newlines in `$` (#843)ehmickybefore adoption1315486
lines 124–147why, one step back124### Multiline commands125126```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```147Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 148why, one step back148### ConcatenationFix script examples (#575)amammadbefore adoption0d038e0
lines 149–151why, one step back149150```sh151# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 152–153why, one step back152tmpDir="/tmp"153mkdir "$tmpDir/filename"Fix script examples (#575)amammadbefore adoption0d038e0
lines 154–157why, one step back154```155156```js157// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 158–159why, one step back158const tmpDir = '/tmp'159await $`mkdir ${tmpDir}/filename`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 160–163why, one step back160```161162```js163// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 164–165why, one step back164const tmpDir = '/tmp'165await $`mkdir ${tmpDir}/filename`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 166–167why, one step back166```167Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 168why, one step back168### Variable substitutionFix script examples (#575)amammadbefore adoption0d038e0
lines 169–171why, one step back169170```sh171# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 172why, one step back172echo $LANGFix script examples (#575)amammadbefore adoption0d038e0
lines 173–176why, one step back173```174175```js176// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 177why, one step back177await $`echo $LANG`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 178–181why, one step back178```179180```js181// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 182why, one step back182await $`echo ${process.env.LANG}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 183–184why, one step back183```184Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 185why, one step back185### EscapingFix script examples (#575)amammadbefore adoption0d038e0
lines 186–188why, one step back186187```sh188# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 189why, one step back189echo 'one two'Fix script examples (#575)amammadbefore adoption0d038e0
lines 190–193why, one step back190```191192```js193// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 194why, one step back194await $`echo ${'one two'}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 195–198why, one step back195```196197```js198// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 199why, one step back199await $`echo ${'one two'}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 200–201why, one step back200```201Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 202why, one step back202### Escaping multiple argumentsFix script examples (#575)amammadbefore adoption0d038e0
lines 203–205why, one step back203204```sh205# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 206why, one step back206echo 'one two' '$'Fix script examples (#575)amammadbefore adoption0d038e0
lines 207–210why, one step back207```208209```js210// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 211why, one step back211await $`echo ${['one two', '$']}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 212–215why, one step back212```213214```js215// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 216why, one step back216await $`echo ${['one two', '$']}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 217–218why, one step back217```218Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 219why, one step back219### SubcommandsFix script examples (#575)amammadbefore adoption0d038e0
lines 220–222why, one step back220221```sh222# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 223why, one step back223echo "$(echo example)"Fix script examples (#575)amammadbefore adoption0d038e0
lines 224–227why, one step back224```225226```js227// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 228–229why, one step back228const example = await $`echo example`;229await $`echo ${example}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 230–233why, one step back230```231232```js233// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 234–235why, one step back234const example = await $`echo example`;235await $`echo ${example}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 236–237why, one step back236```237Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 238why, one step back238### Serial commandsFix script examples (#575)amammadbefore adoption0d038e0
line 239why, one step back239Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 240–244why, one step back240```sh241# Bash242echo one && echo two243```244Fix script examples (#575)amammadbefore adoption0d038e0
lines 245–246why, one step back245```js246// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 247why, one step back247await $`echo one && echo two`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 248–251why, one step back248```249250```js251// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 252–254why, one step back252await $`echo one`;253await $`echo two`;254```Fix script examples (#575)amammadbefore adoption0d038e0
line 255why, one step back255Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 256–261why, one step back256### Parallel commands257258```sh259# Bash260echo one &261echo two &Fix script examples (#575)amammadbefore adoption0d038e0
lines 262–263why, one step back262```263Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 264–267why, one step back264```js265// zx266await Promise.all([$`echo one`, $`echo two`]);267```Fix script examples (#575)amammadbefore adoption0d038e0
line 268why, one step back268Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 269–275why, one step back269```js270// Execa271await Promise.all([$`echo one`, $`echo two`]);272```273274### Global/shared options275Fix script examples (#575)amammadbefore adoption0d038e0
lines 276–277why, one step back276```sh277# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 278–281why, one step back278options="timeout 5"279$options echo one280$options echo two281$options echo threeFix script examples (#575)amammadbefore adoption0d038e0
lines 282–285why, one step back282```283284```js285// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 286–289why, one step back286const 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 back290```291292```js293// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 294–300why, one step back294import {$ 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 back301```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```321Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 322why, one step back322### Local binariesFix script examples (#575)amammadbefore adoption0d038e0
lines 323–325why, one step back323324```sh325# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 326why, one step back326npx tsc --versionFix script examples (#575)amammadbefore adoption0d038e0
lines 327–330why, one step back327```328329```js330// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 331why, one step back331await $`npx tsc --version`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 332–335why, one step back332```333334```js335// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 336why, one step back336await $`tsc --version`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 337–338why, one step back337```338Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 339why, one step back339### Builtin utilitiesFix script examples (#575)amammadbefore adoption0d038e0
line 340why, one step back340Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 341–354why, one step back341```js342// zx343const content = await stdin();344```345346```js347// Execa348import getStdin from 'get-stdin';349350const content = await getStdin();351```352353### Printing to stdout354Fix script examples (#575)amammadbefore adoption0d038e0
lines 355–356why, one step back355```sh356# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 357why, one step back357echo exampleFix script examples (#575)amammadbefore adoption0d038e0
lines 358–361why, one step back358```359360```js361// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 362why, one step back362echo`example`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 363–366why, one step back363```364365```js366// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 367why, one step back367console.log('example');Fix script examples (#575)amammadbefore adoption0d038e0
lines 368–369why, one step back368```369Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 370why, one step back370### Silent stderrFix script examples (#575)amammadbefore adoption0d038e0
lines 371–373why, one step back371372```sh373# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 374why, one step back374echo example 2> /dev/nullFix script examples (#575)amammadbefore adoption0d038e0
lines 375–378why, one step back375```376377```js378// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 379why, one step back379await $`echo example`.stdio('inherit', 'pipe', 'ignore');Fix script examples (#575)amammadbefore adoption0d038e0
lines 380–382why, one step back380```381382```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 383–384why, one step back383// Execa does not print stdout/stderr by default384await $`echo example`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 385–404why, one step back385```386387### Verbose mode388389```sh390# Bash391set -v392echo example393```394395```js396// zx >=8397await $`echo example`.verbose();398399// or:400$.verbose = true;401```402403```js404// ExecaImprove documentation of global script options (#870)ehmickybefore adoption146fa07
lines 405–409why, one step back405import {$ as $_} from 'execa';406407const $ = $_({verbose: true});408409await $`echo example`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 410–417why, one step back410```411412Or:413414```415NODE_DEBUG=execa node file.js416```417Improve `verbose` option (#883)ehmickybefore adoption7d0943f
lines 418–423why, one step back418Which prints:419420```421[19:49:00.360] $ echo example422```423Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 424why, one step back424### Piping stdout to another commandFix script examples (#575)amammadbefore adoption0d038e0
lines 425–427why, one step back425426```sh427# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 428why, one step back428echo npm run build | sort | head -n2Fix script examples (#575)amammadbefore adoption0d038e0
lines 429–432why, one step back429```430431```js432// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 433why, one step back433await $`npm run build | sort | head -n2`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 434–437why, one step back434```435436```js437// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 438–440why, one step back438await $`npm run build`439 .pipe`sort`440 .pipe`head -n2`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 441–442why, one step back441```442Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 443why, one step back443### Piping stdout and stderr to another commandFix script examples (#575)amammadbefore adoption0d038e0
lines 444–446why, one step back444445```sh446# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 447why, one step back447echo example |& catFix script examples (#575)amammadbefore adoption0d038e0
lines 448–451why, one step back448```449450```js451// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 452–456why, one step back452const 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 back457```458459```js460// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 461–462why, one step back461await $({all: true})`echo example`462 .pipe({from: 'all'})`cat`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 463–464why, one step back463```464Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 465why, one step back465### Piping stdout to a fileFix script examples (#575)amammadbefore adoption0d038e0
lines 466–468why, one step back466467```sh468# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 469why, one step back469echo example > file.txtFix script examples (#575)amammadbefore adoption0d038e0
lines 470–473why, one step back470```471472```js473// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 474why, one step back474await $`echo example`.pipe(fs.createWriteStream('file.txt'));Fix script examples (#575)amammadbefore adoption0d038e0
lines 475–478why, one step back475```476477```js478// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 479why, one step back479await $({stdout: {file: 'file.txt'}})`echo example`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 480–481why, one step back480```481Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 482why, one step back482### Piping stdin from a fileFix script examples (#575)amammadbefore adoption0d038e0
lines 483–485why, one step back483484```sh485# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 486why, one step back486echo example < file.txtFix script examples (#575)amammadbefore adoption0d038e0
lines 487–490why, one step back487```488489```js490// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 491–493why, one step back491const cat = $`cat`492fs.createReadStream('file.txt').pipe(cat.stdin)493await catFix script examples (#575)amammadbefore adoption0d038e0
lines 494–497why, one step back494```495496```js497// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 498why, one step back498await $({inputFile: 'file.txt'})`cat`Fix script examples (#575)amammadbefore adoption0d038e0
lines 499–553why, one step back499```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 back554 isTerminated,Fix script examples (#575)amammadbefore adoption0d038e0
lines 555–574why, one step back555 // 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 back575// isTerminated: falseFix script examples (#575)amammadbefore adoption0d038e0
lines 576–578why, one step back576// }577```578Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 579why, one step back579### Exit codesFix script examples (#575)amammadbefore adoption0d038e0
lines 580–582why, one step back580581```sh582# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 583–584why, one step back583false584echo $?Fix script examples (#575)amammadbefore adoption0d038e0
lines 585–588why, one step back585```586587```js588// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 589–590why, one step back589const {exitCode} = await $`false`.nothrow();590echo`${exitCode}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 591–594why, one step back591```592593```js594// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 595–596why, one step back595const {exitCode} = await $({reject: false})`false`;596console.log(exitCode);Fix script examples (#575)amammadbefore adoption0d038e0
lines 597–598why, one step back597```598Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 599why, one step back599### TimeoutsFix script examples (#575)amammadbefore adoption0d038e0
lines 600–602why, one step back600601```sh602# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 603why, one step back603timeout 5 echo exampleFix script examples (#575)amammadbefore adoption0d038e0
lines 604–606why, one step back604```605606```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 607–608why, one step back607// zx608await $`echo example`.timeout('5s');Fix script examples (#575)amammadbefore adoption0d038e0
lines 609–612why, one step back609```610611```js612// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 613why, one step back613await $({timeout: 5000})`echo example`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 614–615why, one step back614```615Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 616why, one step back616### Current filenameFix script examples (#575)amammadbefore adoption0d038e0
lines 617–619why, one step back617618```sh619# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 620why, one step back620echo "$(basename "$0")"Fix script examples (#575)amammadbefore adoption0d038e0
lines 621–624why, one step back621```622623```js624// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 625why, one step back625await $`echo ${__filename}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 626–629why, one step back626```627628```js629// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 630–631why, one step back630import {fileURLToPath} from 'node:url';631import path from 'node:path';Fix script examples (#575)amammadbefore adoption0d038e0
line 632why, one step back632Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 633why, one step back633const __filename = path.basename(fileURLToPath(import.meta.url));Fix script examples (#575)amammadbefore adoption0d038e0
line 634why, one step back634Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 635why, one step back635await $`echo ${__filename}`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 636–637why, one step back636```637Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 638why, one step back638### Current directoryFix script examples (#575)amammadbefore adoption0d038e0
lines 639–641why, one step back639640```sh641# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 642why, one step back642cd projectFix script examples (#575)amammadbefore adoption0d038e0
lines 643–646why, one step back643```644645```js646// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 647–650why, one step back647cd('project');648649// or:650$.cwd = 'project';Fix script examples (#575)amammadbefore adoption0d038e0
lines 651–654why, one step back651```652653```js654// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 655why, one step back655const $$ = $({cwd: 'project'});Fix script examples (#575)amammadbefore adoption0d038e0
lines 656–657why, one step back656```657Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 658why, one step back658### Multiple current directoriesFix script examples (#575)amammadbefore adoption0d038e0
lines 659–661why, one step back659660```sh661# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 662–665why, one step back662pushd project663pwd664popd665pwdFix script examples (#575)amammadbefore adoption0d038e0
lines 666–669why, one step back666```667668```js669// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 670–675why, one step back670within(async () => {671 cd('project');672 await $`pwd`;673});674675await $`pwd`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 676–679why, one step back676```677678```js679// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 680–681why, one step back680await $({cwd: 'project'})`pwd`;681await $`pwd`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 682–683why, one step back682```683Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 684why, one step back684### Background processesFix script examples (#575)amammadbefore adoption0d038e0
lines 685–687why, one step back685686```sh687# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 688why, one step back688echo one &Fix script examples (#575)amammadbefore adoption0d038e0
lines 689–691why, one step back689```690691```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 692why, one step back692// zx does not allow setting the `detached` optionFix script examples (#575)amammadbefore adoption0d038e0
lines 693–696why, one step back693```694695```js696// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 697why, one step back697await $({detached: true})`echo one`;Fix script examples (#575)amammadbefore adoption0d038e0
lines 698–699why, one step back698```699Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 700why, one step back700### PIDFix script examples (#575)amammadbefore adoption0d038e0
lines 701–703why, one step back701702```sh703# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 704–705why, one step back704echo example &705echo $!Fix script examples (#575)amammadbefore adoption0d038e0
lines 706–708why, one step back706```707708```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 709why, one step back709// zx does not return `childProcess.pid`Fix script examples (#575)amammadbefore adoption0d038e0
lines 710–712why, one step back710```711712```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 713–714why, one step back713// Execa714const {pid} = $`echo example`;Fix script examples (#575)amammadbefore adoption0d038e0
line 715why, one step back715```