How to use the parse function from shell-quote

Find comprehensive JavaScript shell-quote.parse code examples handpicked from public code repositorys.

shell-quote.parse is a function that parses a string as a shell command, splitting it into an array of arguments while respecting quotes and escape characters.

2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
if(typeof(str) != "string")
    return;

cl.tprintln("blue", "SSH", "Processing command string '%s' from client at %s...", str, this.stream.session.client.ip);

let argv = ShellQuote.parse(str);
let current_argv = [];

for(const arg of argv)
{
fork icon1
star icon1
watch icon4

+ 4 other calls in file

8
9
10
11
12
13
14
15
16
17
18
19
const COMMON_EDITORS_LINUX = require('./editor-info/linux')
const COMMON_EDITORS_WIN = require('./editor-info/windows')


module.exports = function guessEditor (specifiedEditor) {
  if (specifiedEditor) {
    return shellQuote.parse(specifiedEditor)
  }


  if (process.env.LAUNCH_EDITOR) {
    return [process.env.LAUNCH_EDITOR]
fork icon0
star icon0
watch icon1

+ 4 other calls in file

How does shell-quote.parse work?

In more detail, shell-quote.parse is a function that takes a string as input and splits it into an array of arguments that can be passed to a shell command. The function respects quotes and escape characters, allowing for complex arguments to be correctly parsed. Here are some of the rules that shell-quote.parse follows when parsing a string: Spaces are used to separate arguments. Single quotes (') and double quotes (") can be used to group characters together into an argument. Backslashes () can be used to escape characters that would otherwise have a special meaning, such as quotes and spaces. For example, given the following string: bash Copy code {{{{{{{ class="!whitespace-pre hljs language-bash">echo "Hello, world!" shell-quote.parse would return the following array: css Copy code {{{{{{{ class="!whitespace-pre hljs language-css">['echo', 'Hello, world!'] Because the argument Hello, world! is surrounded by double quotes, it is treated as a single argument, even though it contains a comma and a space. Here's another example: javascript Copy code {{{{{{{ rm -rf ~/Documents/"My Documents"/\*.pdf In this case, shell-quote.parse would return the following array: css Copy code {{{{{{{ class="!whitespace-pre hljs language-css">['rm', '-rf', '~/Documents/My Documents/*.pdf'] Because the argument My Documents is surrounded by double quotes, it is treated as a single argument, even though it contains a space. Additionally, the backslash before the asterisk (*) tells shell-quote.parse to treat it as a literal asterisk, rather than a wildcard character. Overall, shell-quote.parse is a useful function for safely and accurately parsing shell commands that may contain complex arguments.

Ai Example

1
2
3
4
5
6
const shellQuote = require("shell-quote");

const commandString = "ls -l ~/Documents";
const args = shellQuote.parse(commandString);

console.log(args);

In this example, we first require the shell-quote module and then define a string commandString that contains a simple shell command to list the contents of a directory. We then call shellQuote.parse with commandString as the input. shellQuote.parse will split commandString into an array of arguments, respecting quotes and escape characters. In this case, the resulting args array will contain the following elements: css Copy code