You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.6 KiB

6 months ago
  1. # shell-quote <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
  2. [![github actions][actions-image]][actions-url]
  3. [![coverage][codecov-image]][codecov-url]
  4. [![License][license-image]][license-url]
  5. [![Downloads][downloads-image]][downloads-url]
  6. [![npm badge][npm-badge-png]][package-url]
  7. Parse and quote shell commands.
  8. # example
  9. ## quote
  10. ``` js
  11. var quote = require('shell-quote/quote');
  12. var s = quote([ 'a', 'b c d', '$f', '"g"' ]);
  13. console.log(s);
  14. ```
  15. output
  16. ```
  17. a 'b c d' \$f '"g"'
  18. ```
  19. ## parse
  20. ``` js
  21. var parse = require('shell-quote/parse');
  22. var xs = parse('a "b c" \\$def \'it\\\'s great\'');
  23. console.dir(xs);
  24. ```
  25. output
  26. ```
  27. [ 'a', 'b c', '\\$def', 'it\'s great' ]
  28. ```
  29. ## parse with an environment variable
  30. ``` js
  31. var parse = require('shell-quote/parse');
  32. var xs = parse('beep --boop="$PWD"', { PWD: '/home/robot' });
  33. console.dir(xs);
  34. ```
  35. output
  36. ```
  37. [ 'beep', '--boop=/home/robot' ]
  38. ```
  39. ## parse with custom escape character
  40. ``` js
  41. var parse = require('shell-quote/parse');
  42. var xs = parse('beep ^--boop="$PWD"', { PWD: '/home/robot' }, { escape: '^' });
  43. console.dir(xs);
  44. ```
  45. output
  46. ```
  47. [ 'beep --boop=/home/robot' ]
  48. ```
  49. ## parsing shell operators
  50. ``` js
  51. var parse = require('shell-quote/parse');
  52. var xs = parse('beep || boop > /byte');
  53. console.dir(xs);
  54. ```
  55. output:
  56. ```
  57. [ 'beep', { op: '||' }, 'boop', { op: '>' }, '/byte' ]
  58. ```
  59. ## parsing shell comment
  60. ``` js
  61. var parse = require('shell-quote/parse');
  62. var xs = parse('beep > boop # > kaboom');
  63. console.dir(xs);
  64. ```
  65. output:
  66. ```
  67. [ 'beep', { op: '>' }, 'boop', { comment: '> kaboom' } ]
  68. ```
  69. # methods
  70. ``` js
  71. var quote = require('shell-quote/quote');
  72. var parse = require('shell-quote/parse');
  73. ```
  74. ## quote(args)
  75. Return a quoted string for the array `args` suitable for using in shell
  76. commands.
  77. ## parse(cmd, env={})
  78. Return an array of arguments from the quoted string `cmd`.
  79. Interpolate embedded bash-style `$VARNAME` and `${VARNAME}` variables with
  80. the `env` object which like bash will replace undefined variables with `""`.
  81. `env` is usually an object but it can also be a function to perform lookups.
  82. When `env(key)` returns a string, its result will be output just like `env[key]`
  83. would. When `env(key)` returns an object, it will be inserted into the result
  84. array like the operator objects.
  85. When a bash operator is encountered, the element in the array with be an object
  86. with an `"op"` key set to the operator string. For example:
  87. ```
  88. 'beep || boop > /byte'
  89. ```
  90. parses as:
  91. ```
  92. [ 'beep', { op: '||' }, 'boop', { op: '>' }, '/byte' ]
  93. ```
  94. # install
  95. With [npm](http://npmjs.org) do:
  96. ```
  97. npm install shell-quote
  98. ```
  99. # license
  100. MIT
  101. [package-url]: https://npmjs.org/package/shell-quote
  102. [npm-version-svg]: https://versionbadg.es/ljharb/shell-quote.svg
  103. [deps-svg]: https://david-dm.org/ljharb/shell-quote.svg
  104. [deps-url]: https://david-dm.org/ljharb/shell-quote
  105. [dev-deps-svg]: https://david-dm.org/ljharb/shell-quote/dev-status.svg
  106. [dev-deps-url]: https://david-dm.org/ljharb/shell-quote#info=devDependencies
  107. [npm-badge-png]: https://nodei.co/npm/shell-quote.png?downloads=true&stars=true
  108. [license-image]: https://img.shields.io/npm/l/shell-quote.svg
  109. [license-url]: LICENSE
  110. [downloads-image]: https://img.shields.io/npm/dm/shell-quote.svg
  111. [downloads-url]: https://npm-stat.com/charts.html?package=shell-quote
  112. [codecov-image]: https://codecov.io/gh/ljharb/shell-quote/branch/main/graphs/badge.svg
  113. [codecov-url]: https://app.codecov.io/gh/ljharb/shell-quote/
  114. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/shell-quote
  115. [actions-url]: https://github.com/ljharb/shell-quote/actions