Summary of Important Concepts For Quoting and Escaping Windows Command Line Arguments
Quote me as saying I was mis-quoted
- Double quote characters on the command line have no relation to the boundaries between arguments.
- When reading a command line, do not think of a pair of double quote characters ([”…”]) as enclosing some chunk of text. Instead, think of each individual double quote character ([”]) as a switch that toggles the command line parser between two states: recognizing (or interpreting) special characters (InterpretSpecialChars) and ignoring them (IgnoreSpecialChars). Placing double quote characters on the command line is how I define Quoting
- When creating a command line always work from the inside out performing each of the following steps in the sequence shown, independently of the other steps:
- Perform application-specific quoting or escaping requirements
- Ensure correct splitting of command line into arguments by proper placement of individual double quote and escape characters
- Perform “caret” escaping of cmd.exe special characters
- Double up percent signs [%] if the command is to be placed in a batch file
- The first argument on the command line is parsed differently than the rest of the command line by both parsers
- Always specify the executable name as the first argument on the command line in the lpCommandLine parameter passed to CreateProcess. Make sure it’s a correctly-formatted path, and enclose it in double quotes if it contains spaces.
- A program receives a single command line string which the program itself, not the operating system, splits into individual arguments
- When considering a command line, think of quoting as the strategic placement of double quote characters to enable or disable the interpretation of whitespace as an argument separator, not as the delineation of arguments
- There in no standard way to parse a command line, but most programs use one of the following parsers:
- parse_cmdline— called implicitly by CRT during program initialization to generate argv
- CommandLineToArgvW— called explicitly by the programmer
- The difference in behavior between the two parsers is that CommandLineToArgvW returns to InterpretSpecialChars after encountering two double quote characters in a row, while parse_cmdline remains in IgnoreSpecialChars.
- Command lines are split into individual arguments using whitespace as a delimiter
- Whitespace is generally just space (0×20) and horizontal tab (0×09) but includes all characters in the range 0×01 through 0×20 when parsing the first argument with CommandLineToArgvW only