Skip to content
Myr

scripts.md

This file

Path

docs/scripts.md

846 lines · 14.6 KiB · Markdown

Last changed by

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

History of this file

Take it

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

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

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

lines 31–45why, one step back
31## Template string syntax
3233The examples below use the [template string syntax](../readme.md#template-string-syntax). However, the other syntax using [an array of arguments](../readme.md#execafile-arguments-options) is also available as `$(file, arguments?, options?)`.3435Also, the template string syntax can be used outside of script files: `$` is not required to use that syntax. For example, `execa` can use it too.3637The only difference between `$` and `execa` is that the former includes [script-friendly default options](../readme.md#file-arguments-options).3839```js40import {execa, $} from 'execa';4142const branch = await execa`git branch --show-current`;43await $('dep', ['deploy', `--branch=${branch}`]);44```45

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

line 46why, one step back
46## Examples

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 47–98why, one step back
47
48### Main binary4950```sh51# Bash52bash file.sh53```5455```js56// zx57zx file.js5859// or a shebang can be used:60// #!/usr/bin/env zx61```6263```js64// Execa scripts are just regular Node.js files65node file.js66```6768### Global variables6970```js71// zx72await $`echo example`;73```7475```js76// Execa77import {$} from 'execa';7879await $`echo example`;80```8182### Command execution8384```sh85# Bash86echo example87```8889```js90// zx91await $`echo example`;92```9394```js95// Execa96await $`echo example`;97```98

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

lines 99–122why, one step back
99### Multiline commands
100101```sh102# Bash103npm run build \104 --example-flag-one \105 --example-flag-two106```107108```js109// zx110await $`npm run build ${[111 '--example-flag-one',112 '--example-flag-two',113]}`;114```115116```js117// Execa118await $`npm run build119 --example-flag-one120 --example-flag-two`121```122

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 124–126why, one step back
124
125```sh126# Bash

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

lines 127–128why, one step back
127tmpDir="/tmp"
128mkdir "$tmpDir/filename"

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 129–132why, one step back
129```
130131```js132// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 135–138why, one step back
135```
136137```js138// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 141–142why, one step back
141```
142

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 144–146why, one step back
144
145```sh146# Bash

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

line 147why, one step back
147echo $LANG

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 148–151why, one step back
148```
149150```js151// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 153–156why, one step back
153```
154155```js156// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 158–159why, one step back
158```
159

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 161–163why, one step back
161
162```sh163# Bash

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 165–168why, one step back
165```
166167```js168// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 170–173why, one step back
170```
171172```js173// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 175–176why, one step back
175```
176

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 178–180why, one step back
178
179```sh180# Bash

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 182–185why, one step back
182```
183184```js185// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 187–190why, one step back
187```
188189```js190// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 192–193why, one step back
192```
193

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 195–197why, one step back
195
196```sh197# Bash

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 199–202why, one step back
199```
200201```js202// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 205–208why, one step back
205```
206207```js208// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 211–212why, one step back
211```
212

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 214why, one step back
214

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

lines 215–219why, one step back
215```sh
216# Bash217echo one && echo two218```219

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 220–221why, one step back
220```js
221// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 223–226why, one step back
223```
224225```js226// Execa

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

lines 227–229why, one step back
227await $`echo one`;
228await $`echo two`;229```

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 230why, one step back
230

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

lines 231–236why, one step back
231### Parallel commands
232233```sh234# Bash235echo one &236echo two &

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

lines 239–242why, one step back
239```js
240// zx241await Promise.all([$`echo one`, $`echo two`]);242```

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 243why, one step back
243

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

lines 244–250why, one step back
244```js
245// Execa246await Promise.all([$`echo one`, $`echo two`]);247```248249### Global/shared options250

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 251–252why, one step back
251```sh
252# Bash

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

lines 253–256why, one step back
253options="timeout 5"
254$options echo one255$options echo two256$options echo three

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 257–260why, one step back
257```
258259```js260// zx

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

lines 261–264why, one step back
261const timeout = '5s';
262await $`echo one`.timeout(timeout);263await $`echo two`.timeout(timeout);264await $`echo three`.timeout(timeout);

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 265–268why, one step back
265```
266267```js268// Execa

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

lines 269–275why, one step back
269import {$ as $_} from 'execa';
270271const $ = $_({timeout: 5000});272273await $`echo one`;274await $`echo two`;275await $`echo three`;

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 276–296why, one step back
276```
277278### Environment variables279280```sh281# Bash282EXAMPLE=1 example_command283```284285```js286// zx287$.env.EXAMPLE = '1';288await $`example_command`;289delete $.env.EXAMPLE;290```291292```js293// Execa294await $({env: {EXAMPLE: '1'}})`example_command`;295```296

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 298–300why, one step back
298
299```sh300# Bash

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 302–305why, one step back
302```
303304```js305// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 307–310why, one step back
307```
308309```js310// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 312–313why, one step back
312```
313

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 315why, one step back
315

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

lines 316–329why, one step back
316```js
317// zx318const content = await stdin();319```320321```js322// Execa323import getStdin from 'get-stdin';324325const content = await getStdin();326```327328### Printing to stdout329

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 330–331why, one step back
330```sh
331# Bash

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

line 332why, one step back
332echo example

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 333–336why, one step back
333```
334335```js336// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 338–341why, one step back
338```
339340```js341// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 343–344why, one step back
343```
344

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 346–348why, one step back
346
347```sh348# Bash

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 350–353why, one step back
350```
351352```js353// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 355–357why, one step back
355```
356357```js

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 360–379why, one step back
360```
361362### Verbose mode363364```sh365# Bash366set -v367echo example368```369370```js371// zx >=8372await $`echo example`.verbose();373374// or:375$.verbose = true;376```377378```js379// Execa

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

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

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

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

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

lines 384–385why, one step back
384
385await $`echo example`;

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 386–393why, one step back
386```
387388Or:389390```391NODE_DEBUG=execa node file.js392```393

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

lines 394–396why, one step back
394Which prints:
395396```

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

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

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

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

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

lines 400–401why, one step back
400```
401

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 403–405why, one step back
403
404```sh405# Bash

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 407–410why, one step back
407```
408409```js410// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 412–415why, one step back
412```
413414```js415// Execa

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

lines 416–418why, one step back
416await $`npm run build`
417 .pipe`sort`418 .pipe`head -n2`;

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 419–420why, one step back
419```
420

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 422–424why, one step back
422
423```sh424# Bash

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 426–429why, one step back
426```
427428```js429// zx

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

lines 430–434why, one step back
430const echo = $`echo example`;
431const cat = $`cat`;432echo.pipe(cat)433echo.stderr.pipe(cat.stdin);434await Promise.all([echo, cat]);

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 435–438why, one step back
435```
436437```js438// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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 to a file

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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 > file.txt

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 453–456why, one step back
453```
454455```js456// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 458–459why, one step back
458```
459

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 461–463why, one step back
461
462```sh463# Bash

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 465–468why, one step back
465```
466467```js468// zx

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

lines 469–471why, one step back
469const cat = $`cat`
470fs.createReadStream('file.txt').pipe(cat.stdin)471await cat

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 472–475why, one step back
472```
473474```js475// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 477–478why, one step back
477```
478

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 479–505why, one step back
479### Iterate over output lines
480481```sh482# Bash483while read484do485 if [[ "$REPLY" == *ERROR* ]]486 then487 echo "$REPLY"488 fi489done < <(npm run build)490```491492```js493// zx does not allow proper iteration.494// For example, the iteration does not handle subprocess errors.495```496497```js498// Execa499for await (const line of $`npm run build`) {500 if (line.includes('ERROR')) {501 console.log(line);502 }503}504```505

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 506–561why, one step back
506### Errors
507508```sh509# Bash communicates errors only through the exit code and stderr510timeout 1 sleep 2511echo $?512```513514```js515// zx516const {517 stdout,518 stderr,519 exitCode,520 signal,521} = await $`sleep 2`.timeout('1s');522// file:///home/me/Desktop/node_modules/zx/build/core.js:146523// let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);524// ^525// ProcessOutput [Error]:526// at file:///home/me/Desktop/example.js:2:20527// exit code: null528// signal: SIGTERM529// at ChildProcess.<anonymous> (file:///home/me/Desktop/node_modules/zx/build/core.js:146:26)530// at ChildProcess.emit (node:events:512:28)531// at maybeClose (node:internal/child_process:1098:16)532// at Socket.<anonymous> (node:internal/child_process:456:11)533// at Socket.emit (node:events:512:28)534// at Pipe.<anonymous> (node:net:316:12)535// at Pipe.callbackTrampoline (node:internal/async_hooks:130:17) {536// _code: null,537// _signal: 'SIGTERM',538// _stdout: '',539// _stderr: '',540// _combined: ''541// }542```543544```js545// Execa546const {547 stdout,548 stderr,549 exitCode,550 signal,551 signalDescription,552 originalMessage,553 shortMessage,554 command,555 escapedCommand,556 failed,557 timedOut,558 isCanceled,559 isTerminated,560 // and other error-related properties: code, etc.561} = await $({timeout: 1})`sleep 2`;

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 567–568why, one step back
567// command: 'sleep 2',
568// escapedCommand: 'sleep 2',

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

lines 569–575why, one step back
569// cwd: '/path/to/cwd',
570// durationMs: 19.95693,571// failed: true,572// timedOut: true,573// isCanceled: false,574// isTerminated: true,575// signal: 'SIGTERM',

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 576–578why, one step back
576// signalDescription: 'Termination',
577// stdout: '',578// stderr: '',

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

lines 579–580why, one step back
579// stdio: [undefined, '', ''],
580// pipedFrom: []

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 581–583why, one step back
581// }
582```583

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 585–587why, one step back
585
586```sh587# Bash

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

lines 588–589why, one step back
588false
589echo $?

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 590–593why, one step back
590```
591592```js593// zx

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 596–599why, one step back
596```
597598```js599// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 602–603why, one step back
602```
603

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 605–607why, one step back
605
606```sh607# Bash

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 609–611why, one step back
609```
610611```js

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 631–634why, one step back
631```
632633```js634// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 637why, one step back
637

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 639why, one step back
639

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 641–642why, one step back
641```
642

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 644–646why, one step back
644
645```sh646# Bash

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

line 647why, one step back
647cd project

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 648–651why, one step back
648```
649650```js651// zx

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

lines 652–655why, one step back
652cd('project');
653654// or:655$.cwd = 'project';

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 664–666why, one step back
664
665```sh666# Bash

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

lines 667–670why, one step back
667pushd project
668pwd669popd670pwd

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 671–674why, one step back
671```
672673```js674// zx

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

lines 675–680why, one step back
675within(async () => {
676 cd('project');677 await $`pwd`;678});679680await $`pwd`;

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 681–684why, one step back
681```
682683```js684// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

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

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 690–692why, one step back
690
691```sh692# Bash

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

line 693why, one step back
693echo one &

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 694–696why, one step back
694```
695696```js

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 698–701why, one step back
698```
699700```js701// Execa

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 703–704why, one step back
703```
704

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

lines 705–716why, one step back
705### IPC
706707```sh708# Bash does not allow simple IPC709```710711```js712// zx does not allow simple IPC713```714715```js716// Execa

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

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

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

line 718why, one step back
718

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

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

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

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

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

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

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

lines 722–755why, one step back
722 }
723});724```725726### Transforms727728```sh729# Bash does not allow transforms730```731732```js733// zx does not allow transforms734```735736```js737// Execa738const transform = function * (line) {739 if (!line.includes('secret')) {740 yield line;741 }742};743744await $({stdout: [transform, 'inherit']})`echo ${'This is a secret.'}`;745```746747### Cancellation748749```sh750# Bash751kill $PID752```753754```js755// zx

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

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

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

lines 757–761why, one step back
757```
758759```js760// Execa761// Can specify an error message and stack trace

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

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

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

lines 763–784why, one step back
763
764// Or use an `AbortSignal`765const controller = new AbortController();766await $({signal: controller.signal})`node long-script.js`;767```768769### Interleaved output770771```sh772# Bash prints stdout and stderr interleaved773```774775```js776// zx separates stdout and stderr777const {stdout, stderr} = await $`node example.js`;778```779780```js781// Execa can interleave stdout and stderr782const {all} = await $({all: true})`node example.js`;783```784

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 786–788why, one step back
786
787```sh788# Bash

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

lines 789–790why, one step back
789echo example &
790echo $!

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 791–793why, one step back
791```
792793```js

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 795–797why, one step back
795```
796797```js

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

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

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 800why, one step back
800```

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

lines 801–821why, one step back
801
802## Differences with Bash and zx803804This section describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).805806### Flexibility807808Unlike 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).809810### Shell811812The 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.813814This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.815816Also, there is no shell syntax to remember: everything is just plain JavaScript.817818If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.819820### Simplicity821

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 822why, one step back
822Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#file-arguments-options) and [`$(options)`](../readme.md#execaoptions).

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

lines 823–825why, one step back
823
824[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.825

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

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

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

lines 827–841why, one step back
827
828### Modularity829830zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.831832Execa 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.833834### Performance835836Spawning a shell for every command comes at a performance cost, which Execa avoids.837838Also, [local binaries](#local-binaries) can be directly executed without using `npx`.839840### Debugging841

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

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

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

line 843why, one step back
843

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

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

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

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