Skip to content
Myr

escaping.md

This file

Path

docs/escaping.md

100 lines · 3.3 KiB · Markdown

Last changed by

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

History of this file

Take it

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–10why, one step back
1<picture>
2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# 💬 Escaping/quoting89## Array syntax10

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

line 11why, one step back
11When using the [array syntax](execution.md#array-syntax), arguments are automatically escaped. They can contain any character, including spaces, tabs and newlines. However, they cannot contain [null bytes](https://en.wikipedia.org/wiki/Null_character): [binary inputs](binary.md#binary-input) should be used instead. On Windows, they also cannot contain newlines when the file is run through `cmd.exe`, such as `.cmd` and `.bat` files, [among other limitations](windows.md#escaping).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 12–31why, one step back
12
13```js14import {execa} from 'execa';1516await execa('npm', ['run', 'task with space']);17```1819## Template string syntax2021The same applies when using the [template string syntax](execution.md#template-string-syntax). However, spaces, tabs and newlines must use `${}`.2223```js24await execa`npm run ${'task with space'}`;25```2627## User-defined input2829The above syntaxes allow the file and its arguments to be user-defined by passing a variable.3031```js

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 32–34why, one step back
32import {execa} from 'execa';
3334const file = 'npm';

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 35why, one step back
35const commandArguments = ['run', 'task with space'];

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 36why, one step back
36await execa`${file} ${commandArguments}`;

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 37why, one step back
37

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 38why, one step back
38await execa(file, commandArguments);

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 39–40why, one step back
39```
40

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 41why, one step back
41If the file and/or multiple arguments are supplied as a single string, [`parseCommandString()`](api.md#parsecommandstringcommand) can split it into an array.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 42–43why, one step back
42
43```js

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 44why, one step back
44import {execa, parseCommandString} from 'execa';

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 45why, one step back
45

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 46–51why, one step back
46const commandString = 'npm run task';
47const commandArray = parseCommandString(commandString);48await execa`${commandArray}`;4950const [file, ...commandArguments] = commandArray;51await execa(file, commandArguments);

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 52–53why, one step back
52```
53

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 54why, one step back
54Spaces are used as delimiters. They can be escaped with a backslash.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 55–56why, one step back
55
56```js

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 57why, one step back
57await execa`${parseCommandString('npm run task\\ with\\ space')}`;

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 58–61why, one step back
58```
5960## Shells61

Improve documentation about shells (#1060)ehmickybefore adoptionecc00ed

lines 62–66why, one step back
62[Shells](shell.md) ([Bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)), [cmd.exe](https://en.wikipedia.org/wiki/Cmd.exe), etc.) are not used unless the [`shell`](api.md#optionsshell) option is set. This means shell-specific syntax has no special meaning and does not need to be escaped:
63- Quotes: `"value"`, `'value'`, `$'value'`64- Characters: `$variable`, `&&`, `||`, `;`, `|`65- Globbing: `*`, `**`66- Expressions: `$?`, `~`

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 67why, one step back
67

Improve documentation of shell escaping (#1096)ehmickybefore adoptionb959030

lines 68–72why, one step back
68```js
69// This prints `$TASK_NAME`, not `build`70await execa({env: {TASK_NAME: 'build'}})`echo $TASK_NAME`;71```72

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 73–91why, one step back
73If you do set the `shell` option, arguments will not be automatically escaped anymore. Instead, they will be concatenated as a single string using spaces as delimiters.
7475```js76await execa({shell: true})`npm ${'run'} ${'task with space'}`;77// Is the same as:78await execa({shell: true})`npm run task with space`;79```8081Therefore, you need to manually quote the arguments, using the shell-specific syntax.8283```js84await execa({shell: true})`npm ${'run'} ${'"task with space"'}`;85// Is the same as:86await execa({shell: true})`npm run "task with space"`;87```8889Sometimes a shell command is passed as argument to an executable that runs it indirectly. In that case, that shell command must quote its own arguments.9091```js

Improve documentation about shells (#1060)ehmickybefore adoptionecc00ed

lines 92–93why, one step back
92const command = 'npm run "task with space"';
93await execa`ssh host ${command}`;

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 94–100why, one step back
94```
9596<hr>9798[**Next**: 💻 Shell](shell.md)\99[**Previous**: ️▶️ Basic execution](execution.md)\100[**Top**: Table of contents](../readme.md#documentation)