escaping.md
This file
Path
docs/escaping.md
89 lines · 3.1 KiB · Markdown
Last changed by
Improve `readme.md` (#999)ehmickybefore adoptionaca3db3
History of this fileTake it
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–38why, one step back1<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 syntax1011When 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.1213```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```js32const command = 'npm';33const commandArguments = ['run', 'task with space'];3435await execa(command, commandArguments);36await execa`${command} ${commandArguments}`;37```38Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 39why, one step back39However, [`execaCommand()`](api.md#execacommandcommand-options) must be used instead if:Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 40–45why, one step back40- _Both_ the file and its arguments are user-defined41- _And_ those are supplied as a single string4243This is only intended for very specific cases, such as a [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop). This should be avoided otherwise.4445```jsImprove `readme.md` (#999)ehmickybefore adoptionaca3db3
lines 46–47why, one step back46import {execaCommand} from 'execa';47Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 48–60why, one step back48for await (const commandAndArguments of getReplLine()) {49 await execaCommand(commandAndArguments);50}51```5253Arguments passed to `execaCommand()` are automatically escaped. They can contain any character (except [null bytes](https://en.wikipedia.org/wiki/Null_character)), but spaces must be escaped with a backslash.5455```js56await execaCommand('npm run task\\ with\\ space');57```5859## Shells60Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 61why, one step back61[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 characters and expressions (`$variable`, `&&`, `||`, `;`, `|`, etc.) have no special meaning and do not need to be escaped.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 62–89why, one step back6263If 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.6465```js66await execa({shell: true})`npm ${'run'} ${'task with space'}`;67// Is the same as:68await execa({shell: true})`npm run task with space`;69```7071Therefore, you need to manually quote the arguments, using the shell-specific syntax.7273```js74await execa({shell: true})`npm ${'run'} ${'"task with space"'}`;75// Is the same as:76await execa({shell: true})`npm run "task with space"`;77```7879Sometimes 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.8081```js82$`ssh host ${'npm run "task with space"'}`;83```8485<hr>8687[**Next**: 💻 Shell](shell.md)\88[**Previous**: ️▶️ Basic execution](execution.md)\89[**Top**: Table of contents](../readme.md#documentation)