Backslash and quote in command-line arguments

According to this article by Jon Galloway, there can be weird behaviour experienced when using backslashes in command line arguments.

Most notably it mentions that “Most applications (including .NET applications) use CommandLineToArgvW to decode their command lines. It uses crazy escaping rules which explain the behaviour you’re seeing.

It explains that the first set of backslashes do not require escaping, but backslashes coming after alpha (maybe numeric too?) characters require escaping and that quotes always need to be escaped.

Based off of these rules, I believe to get the arguments you want you would have to pass them as:

a "b" "\\x\\\\" "\x\\"

“Whacky” indeed.


The full story of the crazy escaping rules was told in 2011 by an MS blog entry: “Everyone quotes command line arguments the wrong way

Raymond also had something to say on the matter (already back in 2010): “What’s up with the strange treatment of quotation marks and backslashes by CommandLineToArgvW

The situation persists into 2020 and the escaping rules described in Everyone quotes command line arguments the wrong way are still correct as of 2020 and Windows 10.

Leave a Comment