Summary of Important Concepts For Quoting and Escaping Windows Command Line Arguments

Quote me as saying I was mis-quoted

  • 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:
  1. Perform application-specific quoting or escaping requirements
  2. Ensure correct splitting of command line into arguments by proper placement of individual double quote and escape characters
  3. Perform “caret” escaping of cmd.exe special characters
  4. 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:
  1. parse_cmdline— called implicitly by CRT during program initialization to generate argv[]
  2. CommandLineToArgvW— called explicitly by the programmer
  • 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

Post a Comment