Skip to content
Myr

shell.md

This file

Path

docs/shell.md

50 lines · 1.8 KiB · Markdown

Last changed by

Document and test Windows fixes (#1248)Sindre Sorhusbefore adoption4b0edb3

History of this file

Take it

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

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

lines 1–17why, 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# 💻 Shell89## Avoiding shells1011In general, [shells](https://en.wikipedia.org/wiki/Shell_(computing)) should be avoided because they are:12- Not cross-platform, encouraging shell-specific syntax.13- Slower, because of the additional shell interpretation.14- Unsafe, potentially allowing [command injection](https://en.wikipedia.org/wiki/Code_injection#Shell_injection) (see the [escaping section](escaping.md#shells)).1516In almost all cases, plain JavaScript is a better alternative to shells. The [following page](bash.md) shows how to convert Bash into JavaScript.17

Document and test Windows fixes (#1248)Sindre Sorhusbefore adoption4b0edb3

lines 18–27why, one step back
18## When a shell is not needed
1920On Windows, a shell is often used to work around limitations of the OS. This is not needed with Execa. In particular, `shell: true` is not required to:21- run `.cmd` and `.bat` files, such as `npm.cmd`22- [resolve a command](windows.md#file-extensions) by name using `PATHEXT`23- run [shebang](windows.md#shebang) scripts24- [escape](windows.md#escaping) file paths and arguments2526A shell is only needed to use shell-specific syntax, such as pipes `|`, redirections `>`, globbing `*` or environment variable expansion `$VAR`.27

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

lines 28–50why, one step back
28## Specific shell
2930```js31import {execa} from 'execa';3233await execa({shell: '/bin/bash'})`npm run "$TASK" && npm run test`;34```3536## OS-specific shell3738When the [`shell`](api.md#optionsshell) option is `true`, `sh` is used on Unix and [`cmd.exe`](https://en.wikipedia.org/wiki/Cmd.exe) is used on Windows.3940`sh` and `cmd.exe` syntaxes are very different. Therefore, this is usually not useful.4142```js43await execa({shell: true})`npm run build`;44```4546<hr>4748[**Next**: 📜 Scripts](scripts.md)\49[**Previous**: 💬 Escaping/quoting](escaping.md)\50[**Top**: Table of contents](../readme.md#documentation)