Skip to content

Escape a string to use it in shell. Support both linux and windows (cmd and powershell).

License

Notifications You must be signed in to change notification settings

DiscreteTom/esc4sh

Repository files navigation

esc4sh

npm coverage build license

Escape a string to use it in shell. Support both linux and windows (cmd and powershell).

Try it online in the playground.

Install

yarn add esc4sh

Usage

Auto detect os and shell (not working in browser):

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.

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).

import { isWindows, isCmd, isPowershell } from "esc4sh";

isWindows(); // => boolean
isCmd(); // => boolean
isPowershell(); // => boolean

Credit

This project is inspired by xxorax/node-shell-escape and boazy/any-shell-escape.

About

Escape a string to use it in shell. Support both linux and windows (cmd and powershell).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published