esc4sh
v0.1.2
Published
Escape a string to use it in shell. Support both linux and windows (cmd and powershell).
Downloads
16
Readme
esc4sh
Escape a string to use it in shell. Support both linux and windows (cmd and powershell).
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.