escape shell argument in typescript

To escape a shell argument in TypeScript for use with child_process, you can use the shell-escape package.

First, install the package by running:

index.ts
npm install shell-escape
25 chars
2 lines

Then, use it in your TypeScript code like this:

index.ts
import { shellEscape } from 'shell-escape';

const arg = 'some argument with spaces';
const escapedArg = shellEscape([arg]);

// use the escapedArg in your child_process.spawn or child_process.exec calls
204 chars
7 lines

In the example above, the shellEscape function takes an array of arguments and escapes them for use with the shell. The resulting escapedArg variable can then be passed to child_process functions safely.

gistlibby LogSnag