Function Repository Resource:

CommandLineSplit (1.0.0) current version: 1.0.2 »

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.
ResourceFunction["CommandLineSplit"] is compiled to native code using FunctionCompile and as such, fist time evaluation involves this slow compilation step which is then cached using Once.
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 (2) 

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]=

Scope (3) 

Double-quoted substrings are kept together as one token:

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

Extra whitespace is ignored:

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

Empty strings are preserved:

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

Options (2) 

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

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

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

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

Applications (1) 

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

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

Possible Issues (1) 

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

In[9]:=
{
 ResourceFunction["CommandLineSplit"]["echo 'unterminated argument"],
 ResourceFunction["CommandLineSplit"]["echo \"unterminated argument"],
 ResourceFunction["CommandLineSplit"]["cmd trailing\\"]
 }
Out[9]=

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.0 | 2026-04-19

License Information