3 function escapeArgument(arg, quote) {
7 // If we are not going to quote the argument,
8 // escape shell metacharacters, including double and single quotes:
10 arg = arg.replace(/([()%!^<>&|;,"'\s])/g, '^$1');
12 // Sequence of backslashes followed by a double quote:
13 // double up all the backslashes and escape the double quote
14 arg = arg.replace(/(\\*)"/g, '$1$1\\"');
16 // Sequence of backslashes followed by the end of the string
17 // (which will become a double quote later):
18 // double up all the backslashes
19 arg = arg.replace(/(\\*)$/, '$1$1');
21 // All other backslashes occur literally
23 // Quote the whole thing:
24 arg = '"' + arg + '"';
30 module.exports = escapeArgument;