https://github.com/discretetom/esc4sh
Escape a string to use it in shell. Support both linux and windows (cmd and powershell).
https://github.com/discretetom/esc4sh
cmd escape javascript nodejs powershell shell typescript
Last synced: about 2 months ago
JSON representation
Escape a string to use it in shell. Support both linux and windows (cmd and powershell).
- Host: GitHub
- URL: https://github.com/discretetom/esc4sh
- Owner: DiscreteTom
- License: mit
- Created: 2023-11-15T07:16:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-27T13:57:02.000Z (over 1 year ago)
- Last Synced: 2025-03-30T05:03:33.116Z (about 2 months ago)
- Topics: cmd, escape, javascript, nodejs, powershell, shell, typescript
- Language: TypeScript
- Homepage:
- Size: 85.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# esc4sh
[](https://www.npmjs.com/package/esc4sh)


Escape a string to use it in shell. Support both linux and windows (cmd and powershell).
Try it online in the [playground](https://dttk.discretetom.com/js-playground?crushed=%28%27dependencieG%27https%253A%252F%252Fcdn.jsdelivr.net%252FnpmO%25400.1.2%252FdistO.min.js%27%255D%7EcellGJPrepareClib-*BI%28E4cmdM4psM%2520%29NB*N.*Ftrue%7Eid%21D787179%29%252CJCs-*%257B%255C%27%2524PATH%255C%27%257DBBLole.log%257Bs%257DFfalse%7Eid%21K%29%255D%7EpanelGK%255D%29*esc4sh-%2520%253D%2520B%255Cr%255CnC%27%7Ecode%21%27ID1703684E%2520escF%27%7Ereadonly%21Gs%21%255BILt%2520J%28%27name%21%27KD847765LconsM%252CEN-libO%252F*%2501ONMLKJIGFEDCB-*_).
## Install
```bash
yarn add esc4sh
```## Usage
Auto detect os and shell (not working in browser):
```ts
import { Manager } from "esc4sh";const manager = new Manager();
manager.escape("something");// use Array.map to escape multiple strings
// and join them with space to form an executable command
const cmd = ["curl", ...options.map((o) => manager.escape(o))].join(" ");
```Explicitly specify shell. These can be used in browser.
```ts
import { esc4sh, esc4cmd, esc4ps, esc } from "esc4sh";// linux sh/bash
console.log(esc4sh("$PATH")); // => '$PATH'
// windows cmd
console.log(esc4cmd("%PATH$")); // => "%PATH%"
// windows powershell
console.log(esc4ps("$env:PATH")); // => '$env:PATH'// escape by options
esc(""); // for linux sh/bash
esc("", { windows: true }); // for windows cmd
esc("", { windows: true, powershell: true }); // for windows powershell
```Helpers (not working in browser).
```ts
import { isWindows, isCmd, isPowershell } from "esc4sh";isWindows(); // => boolean
isCmd(); // => boolean
isPowershell(); // => boolean
```## Credit
This project is inspired by [xxorax/node-shell-escape](https://github.com/xxorax/node-shell-escape/) and [boazy/any-shell-escape](https://github.com/boazy/any-shell-escape/).
## [CHANGELOG](./CHANGELOG.md)