Function Repository Resource:

CommandLineSplit

Source Notebook

Split a command-line string respecting quotes and escape characters

Contributed by: Antonis Aristeidou

ResourceFunction["CommandLineSplit"][str]

splits the string str respecting quotes and escape characters.

Details and Options

With the default options, ResourceFunction["CommandLineSplit"] is fully POSIX compliant.
Commands are split based on the operators "||", "&&", "|", "&", ";", "\n" and "\r".
If no quotations or escapes are present in the string, ResourceFunction["CommandLineSplit"] falls back to StringSplit.
ResourceFunction["CommandLineSplit"] takes the following options:
"TokenDelimiters"{" ", "\t"}sets the delimiter between tokens
"EscapeCharacter""\\"sets the escape character

Examples

Basic Examples (3) 

CommandLineSplit will break up a string into constituent argv elements keeping strings as a single unit:

In[1]:=
ResourceFunction["CommandLineSplit"]["echo 'Hello World!'"]
Out[1]=

Escape characters within strings are supported:

In[2]:=
ResourceFunction["CommandLineSplit"]["echo 'Hello \"World!\"'"]
Out[2]=

Multiple commands are parsed separately with the separators being retained as separate elements:

In[3]:=
ResourceFunction[
 "CommandLineSplit"]["echo 'Hello World!'; echo 'Hello again World!' | grep 'again';"]
Out[3]=

Scope (4) 

Double-quoted substrings are kept together as one token:

In[4]:=
ResourceFunction["CommandLineSplit"]["grep \"hello world\" file.txt"]
Out[4]=

Escaped command separators are ignored:

In[5]:=
ResourceFunction["CommandLineSplit"]["grep \"\\& hello \\;\""]
Out[5]=

Extra whitespace is ignored:

In[6]:=
ResourceFunction["CommandLineSplit"]["python   script.py    --flag"]
Out[6]=

Empty strings are preserved:

In[7]:=
ResourceFunction["CommandLineSplit"]["cmd \"\" ''"]
Out[7]=

Options (2) 

An equals sign can be treated as an additional delimiter using the "TokenDelimiters" option:

In[8]:=
ResourceFunction[
 "CommandLineSplit"]["program --mode=fast --output result.txt", "TokenDelimiters" -> ("=" | " ")]
Out[8]=

A different escape character can be specified with "EscapeCharacter":

In[9]:=
ResourceFunction["CommandLineSplit"]["say ^\"hello world^\"", "EscapeCharacter" -> "^"]
Out[9]=

Applications (1) 

Split a simple command into a form suitable for further processing:

In[10]:=
cmd = ResourceFunction["CommandLineSplit"][
  "wolframscript -code \"2 + 2\""]
Out[10]=

Possible Issues (1) 

Un-terminated quoted strings or escape sequences produce a Message and return a Failure:

In[11]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/766b16b0-6be6-4a20-ab48-ff2b3b3b18da"]
Out[11]=

Publisher

Antonis Aristeidou

Requirements

Wolfram Language 12.3 (May 2021) or above

Version History

  • 1.0.2 – 01 May 2026
  • 1.0.0 – 24 April 2026

Related Resources

Author Notes

1.0.2 | 2026-04-27

- Add multi-command support.

1.0.1 | 2026-04-24

- Include pre-compiled IR directly.

License Information