Search notes:

cmd.exe: Splitting command line into arguments when executing an exe

showArguments.c

The following simple c program makes it possible to analyse how a command line is parsed and which arguments are passed to an executable when invoked from cmd.exe:
//
//  cl /nologo /FeshowArguments.exe   showArguments.c
//  cl /nologo /FeExpandArguments.exe showArguments.c /link setargv.obj
//
#include <stdio.h>

int main(int argc, char *argv[]) {

    int i;
    for (i=1; i<argc; i++) {
      printf("argv[%2d] = %s\n", i, argv[i]);
    }

}
Github repository about-cmd.exe, path: /parse-command-line/exe/showArguments.c
Below, there are some findings from experimenting with this program.

Seperating arguments by white space

Fundamentally, the arguments are separated by white space (any consecutive combination of spaces or tabs):
showArguments.exe foo bar baz
argv[ 1] = foo
argv[ 2] = bar
argv[ 3] = baz

Including white space into arguments

By enclosing words into pairs of double quotes, the words including the white space become one argument:
showArguments.exe "this is one argument" "This is the 2nd argument" "The 3rd"
argv[ 1] = this is one argument
argv[ 2] = This is the 2nd argument
argv[ 3] = The 3rd

Escaping double quotes

A double quote is escaped with a backslash:
showArguments.exe \" "the 2nd arg" "An arg containing an \"." four
argv[ 1] = "
argv[ 2] = the 2nd arg
argv[ 3] = An arg containing an ".
argv[ 4] = four

Backslashes only escape double-quotes

Backslashes only escape double-quotes (and sometimes themselves). They have no special meaning when preceding other characters:
showArguments.exe \ \\ \\\ \\\\four \t \n \x \"
argv[ 1] = \
argv[ 2] = \\
argv[ 3] = \\\
argv[ 4] = \\\\four
argv[ 5] = \t
argv[ 6] = \n
argv[ 7] = \x
argv[ 8] = "
A special case is when consecutive backslashes are immediately followed by a double quote.
If the number of backslashes is even, then the backslashes escape themselves:
showArguments.exe AAA \\" BBB \\ bbb \\\\" \\\\" CCC \\ ccc \\" DDD
argv[ 1] = AAA
argv[ 2] = \ BBB \\ bbb \\
argv[ 3] = \\ CCC \\ ccc \
argv[ 4] = DDD
If the number of backslashes is odd, then the backslashes escape themselves and the following double-quote:
showArguments.exe xxx \\\" yyy \\\\\" zzz
argv[ 1] = xxx
argv[ 2] = \"
argv[ 3] = yyy
argv[ 4] = \\"
argv[ 5] = zzz

Special case: enclosing one word into double quotes

A special case seems to be if one word is enclosed into double quotes.
In this case, the double quotes are just skipped
showArguments.exe "arg One" "Did he say "hello"?" "arg three"
argv[ 1] = arg One
argv[ 2] = Did he say hello?
argv[ 3] = arg three
If "hello" is replaced with "hello world" in the preceding example, the output becomes as follows. It's not clear to me what rule is responsible for that:
showArguments.exe "arg One" "Did he say "hello world"?" "arg three"
argv[ 1] = arg One
argv[ 2] = Did he say hello
argv[ 3] = world?
argv[ 4] = arg three

Star and question mark

If not linked with setargv.obj, stars and questionmarks are not treated specially:
showArguments * ?
argv[ 1] = *
argv[ 2] = ?

Environment variables

Environment variables are expanded, both within quoted strings and otherwise:
showArguments "Temp = %temp%" %userprofile%
argv[ 1] = Temp = C:\Users\Rene\AppData\Local\Temp
argv[ 2] = C:\Users\Rene

Caret symbol

One caret symbol (^) is just skipped when the command line is parsed:
showArguments one ^ two
argv[ 1] = one
argv[ 2] = two
A caret symbol followed by a bar, ampersand or another caret symbol escapes it:
showArguments one ^^ three
argv[ 1] = one
argv[ 2] = ^
argv[ 3] = three
showArguments bar: ^| ampersand: ^& caret: ^^ end
argv[ 1] = bar:
argv[ 2] = |
argv[ 3] = ampersand:
argv[ 4] = &
argv[ 5] = caret:
argv[ 6] = ^
argv[ 7] = end
If the caret symbol is placed between a percent sign and a letter, it makes it possible to »pass« the »name« of environment variables to a program:
showArguments.exe "%^Temp% = %temp%" %^userprofile%
argv[ 1] = %^Temp% = C:\Users\Rene\AppData\Local\Temp
argv[ 2] = %userprofile%
Note: the caret is also used as line continuation character.

See also

Parse the command line
Programs and scripts that show command line arguments

Links

A better way to understand quoting and escaping of Windows Command Line Arguments. The main points of the articles are:

Index