UNPKG

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