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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101<picture> <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg"> <img alt="execa logo" src="../media/logo.svg" width="400"></picture><br>
# 💬 Escaping/quoting
## Array syntax
When 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).
```jsimport {execa} from 'execa';
await execa('npm', ['run', 'task with space']);```
## Template string syntax
The same applies when using the [template string syntax](execution.md#template-string-syntax). However, spaces, tabs and newlines must use `${}`.
```jsawait execa`npm run ${'task with space'}`;```
## User-defined input
The above syntaxes allow the file and its arguments to be user-defined by passing a variable.
```jsimport {execa} from 'execa';
const file = 'npm';const commandArguments = ['run', 'task with space'];await execa`${file} ${commandArguments}`;
await execa(file, commandArguments);```
If the file and/or multiple arguments are supplied as a single string, [`parseCommandString()`](api.md#parsecommandstringcommand) can split it into an array.
```jsimport {execa, parseCommandString} from 'execa';
const commandString = 'npm run task';const commandArray = parseCommandString(commandString);await execa`${commandArray}`;
const [file, ...commandArguments] = commandArray;await execa(file, commandArguments);```
Spaces are used as delimiters. They can be escaped with a backslash.
```jsawait execa`${parseCommandString('npm run task\\ with\\ space')}`;```
## Shells
[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:- Quotes: `"value"`, `'value'`, `$'value'`- Characters: `$variable`, `&&`, `||`, `;`, `|`- Globbing: `*`, `**`- Expressions: `$?`, `~`
```js// This prints `$TASK_NAME`, not `build`await execa({env: {TASK_NAME: 'build'}})`echo $TASK_NAME`;```
If 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.
```jsawait execa({shell: true})`npm ${'run'} ${'task with space'}`;// Is the same as:await execa({shell: true})`npm run task with space`;```
Therefore, you need to manually quote the arguments, using the shell-specific syntax.
```jsawait execa({shell: true})`npm ${'run'} ${'"task with space"'}`;// Is the same as:await execa({shell: true})`npm run "task with space"`;```
Sometimes 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.
```jsconst command = 'npm run "task with space"';await execa`ssh host ${command}`;```
<hr>
[**Next**: 💻 Shell](shell.md)\[**Previous**: ️▶️ Basic execution](execution.md)\[**Top**: Table of contents](../readme.md#documentation)