UNPKG

2.61 kBMarkdownView Raw
1# shell-quote
2
3Parse and quote shell commands.
4
5[![build status](https://secure.travis-ci.org/substack/node-shell-quote.png)](http://travis-ci.org/substack/node-shell-quote)
6
7[![browser support](https://ci.testling.com/substack/node-shell-quote.png)](https://ci.testling.com/substack/node-shell-quote)
8
9# example
10
11## quote
12
13``` js
14var quote = require('shell-quote').quote;
15var s = quote([ 'a', 'b c d', '$f', '"g"' ]);
16console.log(s);
17```
18
19output
20
21```
22a 'b c d' \$f '"g"'
23```
24
25## parse
26
27``` js
28var parse = require('shell-quote').parse;
29var xs = parse('a "b c" \\$def \'it\\\'s great\'');
30console.dir(xs);
31```
32
33output
34
35```
36[ 'a', 'b c', '\\$def', 'it\'s great' ]
37```
38
39## parse with an environment variable
40
41``` js
42var parse = require('shell-quote').parse;
43var xs = parse('beep --boop="$PWD"', { PWD: '/home/robot' });
44console.dir(xs);
45```
46
47output
48
49```
50[ 'beep', '--boop=/home/robot' ]
51```
52
53## parse with custom escape charcter
54
55``` js
56var parse = require('shell-quote').parse;
57var xs = parse('beep --boop="$PWD"', { PWD: '/home/robot' }, { escape: '^' });
58console.dir(xs);
59```
60
61output
62
63```
64[ 'beep', '--boop=/home/robot' ]
65```
66
67## parsing shell operators
68
69``` js
70var parse = require('shell-quote').parse;
71var xs = parse('beep || boop > /byte');
72console.dir(xs);
73```
74
75output:
76
77```
78[ 'beep', { op: '||' }, 'boop', { op: '>' }, '/byte' ]
79```
80
81## parsing shell comment
82
83``` js
84var parse = require('shell-quote').parse;
85var xs = parse('beep > boop # > kaboom');
86console.dir(xs);
87```
88
89output:
90
91```
92[ 'beep', { op: '>' }, 'boop', { comment: '> kaboom' } ]
93```
94
95# methods
96
97``` js
98var quote = require('shell-quote').quote;
99var parse = require('shell-quote').parse;
100```
101
102## quote(args)
103
104Return a quoted string for the array `args` suitable for using in shell
105commands.
106
107## parse(cmd, env={})
108
109Return an array of arguments from the quoted string `cmd`.
110
111Interpolate embedded bash-style `$VARNAME` and `${VARNAME}` variables with
112the `env` object which like bash will replace undefined variables with `""`.
113
114`env` is usually an object but it can also be a function to perform lookups.
115When `env(key)` returns a string, its result will be output just like `env[key]`
116would. When `env(key)` returns an object, it will be inserted into the result
117array like the operator objects.
118
119When a bash operator is encountered, the element in the array with be an object
120with an `"op"` key set to the operator string. For example:
121
122```
123'beep || boop > /byte'
124```
125
126parses as:
127
128```
129[ 'beep', { op: '||' }, 'boop', { op: '>' }, '/byte' ]
130```
131
132# install
133
134With [npm](http://npmjs.org) do:
135
136```
137npm install shell-quote
138```
139
140# license
141
142MIT