scripts.md
This file
Path
docs/scripts.md
709 lines · 11.3 KiB · Markdown
Last changed by
Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
History of this fileTake it
The walk stopped after 200 commits: lines older than db2ad9a point at it.
Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 10–123why, 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.4849Execa 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), [`signal`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), etc.5051### 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### ConcatenationImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
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"Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 166–167why, one step back166```167Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 168why, one step back168### Variable substitutionFix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 169–171why, one step back169170```sh171# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 172why, one step back172echo $LANGFix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
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 argument concatenation with `$` (#553)ehmickybefore adoption07585d0
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 argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 183–184why, one step back183```184Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 185why, one step back185### EscapingImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 186–188why, one step back186187```sh188# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 189why, one step back189echo 'one two'Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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'}`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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'}`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 200–201why, one step back200```201Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 202why, one step back202### Escaping multiple argumentsImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 203–205why, one step back203204```sh205# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 206why, one step back206echo 'one two' '$'Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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', '$']}`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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', '$']}`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 217–218why, one step back217```218Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 219why, one step back219### SubcommandsImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 220–222why, one step back220221```sh222# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 223why, one step back223echo "$(echo example)"Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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}`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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}`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 236–237why, one step back236```237Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 238why, one step back238### Serial commandsImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
line 239why, one step back239Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 240–244why, one step back240```sh241# Bash242echo one && echo two243```244Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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```Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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 &Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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```Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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 options275Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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 threeImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
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);Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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 binariesImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 323–325why, one step back323324```sh325# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 326why, one step back326npx tsc --versionImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
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`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 332–335why, one step back332```333334```js335// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 336why, one step back336await $`tsc --version`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 337–338why, one step back337```338Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 339why, one step back339### Builtin utilitiesImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
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 stdout354Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 355–356why, one step back355```sh356# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 357why, one step back357echo exampleImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 358–361why, one step back358```359360```js361// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 362why, one step back362echo`example`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 363–366why, one step back363```364365```js366// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 367why, one step back367console.log('example');Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 368–369why, one step back368```369Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 370why, one step back370### Silent stderrImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 371–373why, one step back371372```sh373# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 374why, one step back374echo example 2> /dev/nullImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
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');Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
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`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 410–417why, one step back410```411412Or:413414```415NODE_DEBUG=execa node file.js416```417Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 418why, one step back418### Piping stdout to another commandImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 419–421why, one step back419420```sh421# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 422why, one step back422echo npm run build | sort | head -n2Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 423–426why, one step back423```424425```js426// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 427why, one step back427await $`npm run build | sort | head -n2`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 428–431why, one step back428```429430```js431// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 432–434why, one step back432await $`npm run build`433 .pipe`sort`434 .pipe`head -n2`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 435–436why, one step back435```436Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 437why, one step back437### Piping stdout and stderr to another commandImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 438–440why, one step back438439```sh440# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 441why, one step back441echo example |& catImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 442–445why, one step back442```443444```js445// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 446–450why, one step back446const echo = $`echo example`;447const cat = $`cat`;448echo.pipe(cat)449echo.stderr.pipe(cat.stdin);450await Promise.all([echo, cat]);Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 451–454why, one step back451```452453```js454// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 455–456why, one step back455await $({all: true})`echo example`456 .pipe({from: 'all'})`cat`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 457–458why, one step back457```458Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 459why, one step back459### Piping stdout to a fileImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 460–462why, one step back460461```sh462# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 463why, one step back463echo example > file.txtImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 464–467why, one step back464```465466```js467// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 468why, one step back468await $`echo example`.pipe(fs.createWriteStream('file.txt'));Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 469–472why, one step back469```470471```js472// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 473why, one step back473await $({stdout: {file: 'file.txt'}})`echo example`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 474–475why, one step back474```475Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 476why, one step back476### Piping stdin from a fileImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 477–479why, one step back477478```sh479# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 480why, one step back480echo example < file.txtImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 481–484why, one step back481```482483```js484// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 485–487why, one step back485const cat = $`cat`486fs.createReadStream('file.txt').pipe(cat.stdin)487await catImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 488–491why, one step back488```489490```js491// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 492why, one step back492await $({inputFile: 'file.txt'})`cat`Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 493–547why, one step back493```494495### Errors496497```sh498# Bash communicates errors only through the exit code and stderr499timeout 1 sleep 2500echo $?501```502503```js504// zx505const {506 stdout,507 stderr,508 exitCode,509 signal,510} = await $`sleep 2`.timeout('1s');511// file:///home/me/Desktop/node_modules/zx/build/core.js:146512// let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);513// ^514// ProcessOutput [Error]:515// at file:///home/me/Desktop/example.js:2:20516// exit code: null517// signal: SIGTERM518// at ChildProcess.<anonymous> (file:///home/me/Desktop/node_modules/zx/build/core.js:146:26)519// at ChildProcess.emit (node:events:512:28)520// at maybeClose (node:internal/child_process:1098:16)521// at Socket.<anonymous> (node:internal/child_process:456:11)522// at Socket.emit (node:events:512:28)523// at Pipe.<anonymous> (node:net:316:12)524// at Pipe.callbackTrampoline (node:internal/async_hooks:130:17) {525// _code: null,526// _signal: 'SIGTERM',527// _stdout: '',528// _stderr: '',529// _combined: ''530// }531```532533```js534// Execa535const {536 stdout,537 stderr,538 exitCode,539 signal,540 signalDescription,541 originalMessage,542 shortMessage,543 command,544 escapedCommand,545 failed,546 timedOut,547 isCanceled,Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c
line 548why, one step back548 isTerminated,Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 549–568why, one step back549 // and other error-related properties: code, etc.550} = await $({timeout: 1})`sleep 2`;551// file:///home/me/code/execa/lib/kill.js:60552// reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));553// ^554// Error: Command timed out after 1 milliseconds: sleep 2555// Timed out556// at file:///home/me/Desktop/example.js:2:20557// timedOut: true,558// signal: 'SIGTERM',559// originalMessage: 'Timed out',560// shortMessage: 'Command timed out after 1 milliseconds: sleep 2\nTimed out',561// command: 'sleep 2',562// escapedCommand: 'sleep 2',563// exitCode: undefined,564// signalDescription: 'Termination',565// stdout: '',566// stderr: '',567// failed: true,568// isCanceled: false,Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c
line 569why, one step back569// isTerminated: falseImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 570–572why, one step back570// }571```572Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 573why, one step back573### Exit codesImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 574–576why, one step back574575```sh576# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 577–578why, one step back577false578echo $?Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 579–582why, one step back579```580581```js582// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 583–584why, one step back583const {exitCode} = await $`false`.nothrow();584echo`${exitCode}`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 585–588why, one step back585```586587```js588// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 589–590why, one step back589const {exitCode} = await $({reject: false})`false`;590console.log(exitCode);Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 591–592why, one step back591```592Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 593why, one step back593### TimeoutsImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 594–596why, one step back594595```sh596# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 597why, one step back597timeout 5 echo exampleImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 598–600why, one step back598```599600```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 601–602why, one step back601// zx602await $`echo example`.timeout('5s');Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 603–606why, one step back603```604605```js606// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 607why, one step back607await $({timeout: 5000})`echo example`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 608–609why, one step back608```609Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 610why, one step back610### Current filenameImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 611–613why, one step back611612```sh613# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 614why, one step back614echo "$(basename "$0")"Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 615–618why, one step back615```616617```js618// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 619why, one step back619await $`echo ${__filename}`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 620–623why, one step back620```621622```js623// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 624–625why, one step back624import {fileURLToPath} from 'node:url';625import path from 'node:path';Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
line 626why, one step back626Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 627why, one step back627const __filename = path.basename(fileURLToPath(import.meta.url));Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
line 628why, one step back628Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 629why, one step back629await $`echo ${__filename}`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 630–631why, one step back630```631Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 632why, one step back632### Current directoryImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 633–635why, one step back633634```sh635# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 636why, one step back636cd projectImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 637–640why, one step back637```638639```js640// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 641–644why, one step back641cd('project');642643// or:644$.cwd = 'project';Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 645–648why, one step back645```646647```js648// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 649why, one step back649const $$ = $({cwd: 'project'});Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 650–651why, one step back650```651Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 652why, one step back652### Multiple current directoriesImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 653–655why, one step back653654```sh655# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 656–659why, one step back656pushd project657pwd658popd659pwdImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 660–663why, one step back660```661662```js663// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 664–669why, one step back664within(async () => {665 cd('project');666 await $`pwd`;667});668669await $`pwd`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 670–673why, one step back670```671672```js673// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 674–675why, one step back674await $({cwd: 'project'})`pwd`;675await $`pwd`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 676–677why, one step back676```677Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 678why, one step back678### Background processesImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 679–681why, one step back679680```sh681# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 682why, one step back682echo one &Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 683–685why, one step back683```684685```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 686why, one step back686// zx does not allow setting the `detached` optionImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 687–690why, one step back687```688689```js690// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 691why, one step back691await $({detached: true})`echo one`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 692–693why, one step back692```693Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 694why, one step back694### PIDImprove main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 695–697why, one step back695696```sh697# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 698–699why, one step back698echo example &699echo $!Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 700–702why, one step back700```701702```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 703why, one step back703// zx does not return `childProcess.pid`Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
lines 704–706why, one step back704```705706```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 707–708why, one step back707// Execa708const {pid} = $`echo example`;Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a
line 709why, one step back709```