escaping.md
This file
Path
docs/escaping.md
95 lines · 3.1 KiB · Markdown
Last changed by
Improve documentation about shells (#1060)ehmickybefore adoptionecc00ed
History of this fileTake it
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–31why, 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```jsAdd `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 32–34why, one step back32import {execa} from 'execa';3334const file = 'npm';Add user guides (#989)ehmickybefore adoptionbe91ecf
line 35why, one step back35const commandArguments = ['run', 'task with space'];Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 36why, one step back36await execa`${file} ${commandArguments}`;Add user guides (#989)ehmickybefore adoptionbe91ecf
line 37why, one step back37Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 38why, one step back38await execa(file, commandArguments);Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 39–40why, one step back39```40Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 41why, one step back41If 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 back4243```jsAdd `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 44why, one step back44import {execa, parseCommandString} from 'execa';Improve `readme.md` (#999)ehmickybefore adoptionaca3db3
line 45why, one step back45Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 46–51why, one step back46const 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 back52```53Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 54why, one step back54Spaces are used as delimiters. They can be escaped with a backslash.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 55–56why, one step back5556```jsAdd `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 57why, one step back57await execa`${parseCommandString('npm run task\\ with\\ space')}`;Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 58–61why, one step back58```5960## Shells61Improve documentation about shells (#1060)ehmickybefore adoptionecc00ed
lines 62–66why, one step back62[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
lines 67–86why, one step back6768If 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.6970```js71await execa({shell: true})`npm ${'run'} ${'task with space'}`;72// Is the same as:73await execa({shell: true})`npm run task with space`;74```7576Therefore, you need to manually quote the arguments, using the shell-specific syntax.7778```js79await execa({shell: true})`npm ${'run'} ${'"task with space"'}`;80// Is the same as:81await execa({shell: true})`npm run "task with space"`;82```8384Sometimes 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.8586```jsImprove documentation about shells (#1060)ehmickybefore adoptionecc00ed
lines 87–88why, one step back87const command = 'npm run "task with space"';88await execa`ssh host ${command}`;Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 89–95why, one step back89```9091<hr>9293[**Next**: 💻 Shell](shell.md)\94[**Previous**: ️▶️ Basic execution](execution.md)\95[**Top**: Table of contents](../readme.md#documentation)