# shlex

> Node.js port of Python's shlex shell-like lexer

Latest version **3.0.0** (published 2025-06-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install shlex
pnpm add shlex
yarn add shlex
bun add shlex
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2025-06-28 |
| First published | 2018-06-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 20.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 42 |
| Author | Ryan Govostes |
| Maintainers | rgov |
| Keywords | shell, command-line, cli, lexer |

## Links

- npm: https://www.npmjs.com/package/shlex
- Repository: https://github.com/rgov/node-shlex
- Homepage: https://github.com/rgov/node-shlex#readme
- Issues: https://github.com/rgov/node-shlex/issues
- npm.io page: https://npm.io/package/shlex

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2025-06-28
- 2.1.2 — 2022-06-24
- 2.1.1 — 2022-06-24
- 2.1.0 — 2021-05-25
- 2.0.2 — 2020-04-06
- 2.0.1 — 2019-03-25
- 2.0.0 — 2019-03-24
- 1.0.3 — 2018-09-06
- 1.0.2 — 2018-06-24
- 1.0.1 — 2018-06-19
- 1.0.0 — 2018-06-19

## README

# node-shlex

![Build Status](https://github.com/rgov/node-shlex/workflows/Node.js%20CI/badge.svg)

`node-shlex` is a JavaScript module for quoting and parsing shell commands.

The API was inspired by the [`shlex`][pyshlex] module from the Python Standard 
Library. However, the Python implementation is fairly complex, and supports a
confusing matrix of modes that is not replicated here. `node-shlex` always
operates in what the Python module calls "POSIX mode."

[pyshlex]: https://docs.python.org/3/library/shlex.html

As of version 2.0.0, Bash's [ANSI C strings][ansi-c] (`$'x'`) and
[locale-specific translation strings][locale] (`$"x"`) are supported. This
diverges from the Python `shlex` behavior but makes parsing more accurate.

[ansi-c]: https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
[locale]: https://www.gnu.org/software/bash/manual/html_node/Locale-Translation.html

Note that `node-shlex` does not attempt to split on or otherwise parse 
operators (such as `2>/dev/null`) or comments, and it does not perform variable interpolation.

## Usage

### `shlex.join()`

```node
import { join } from 'shlex'
join(["ls", "-al", '/'])  // returns: 'ls -al /'
join([ 'rm', '-f', '/Volumes/Macintosh HD' ])  // returns: "rm -f '/Volumes/Macintosh HD'"
```

### `shlex.quote()`

```node
import { quote } from 'shlex'
quote("abc")      // returns: abc
quote("abc def")  // returns: 'abc def'
quote("can't")    // returns: 'can'"'"'t'
```

### `shlex.split()`

```node
import { split } from 'shlex'
split('ls -al /')  // returns: [ 'ls', '-al', '/' ]
split('rm -f "/Volumes/Macintosh HD"')  // returns: [ 'rm', '-f', '/Volumes/Macintosh HD' ]
```

---
_Source: https://npm.io/package/shlex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
