2

With this program (cs.exe):

class Program
{
    static void Main(string[] args)
    {
        foreach (var item in args)
        {
            Console.WriteLine(item);
        }
    }
}

And run:

> cs.exe go\to\a_path
go\to\a_path

> cs.exe "go\to\a path"
go\to\a path

> cs.exe "go\to\a path\"
go\to\a path"

> cs.exe 'go\to\a path\'
'go\to\a
path\'

That means if your path has a space so you quote it, be very careful NOT to put a trailing \ at the end, otherwise your program might just not be able to handle it as it incorrectly contains a " at the end. Single quote is even weirder!

PowerShell exhibits a similar behavior but without the difference between single and double quotes.

How do I understand this behavior? What's the underlying rule to evaluate backslash in cmd so this can be explained consistently?

6
  • Note that using a trailing backslash in a path name is unusual, so this behaviour usually isn't a problem. Also that if your program needs to cope with trailing backslashes, you can parse the command line yourself, with whatever rules you deem most convenient. Jan 12, 2017 at 8:41
  • @HarryJohnston it's really not that unusual. PowerShell autocomplete always put a trailing backslash when you press tab on a directory name. This is how I found it in the first place.
    – KFL
    Jan 12, 2017 at 16:59
  • Not on my machine it doesn't! Is that a Windows 10 thing, perhaps? Jan 12, 2017 at 20:34
  • Yes, another black mark for Windows 10. That's really going to be a pain. Jan 12, 2017 at 20:41
  • @HarryJohnston should be the version of PowerShell that I use. I don't blame it. I would have appended / at the end of any directory path myself as a clear note that it's a directory. I think it's just a limitation of the command line interpretation protocol @MC ND explained below, unfortunately.
    – KFL
    Jan 12, 2017 at 21:40

1 Answer 1

6

As you are not calling an internal cmd command, but calling an executable file, this behaviour is not caused by cmd but the command line argument parser routines. In windows, programs don't receive a collection/array/set of arguments, but a string with all the arguments and each program tokenizes this string to obtain each element. This is usually done by routines included by the compiler that hides this operation and exposes to the code an easier way to handle arguments.

Documentation for the C Command-Line argument parser states that

  • Arguments are delimited by white space, which is either a space or a tab.

  • A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument. Note that the caret (^) is not recognized as an escape character or delimiter.

  • A double quotation mark preceded by a backslash, \", is interpreted as a literal double quotation mark (").

  • Backslashes are interpreted literally, unless they immediately precede a double quotation mark.

  • If an even number of backslashes is followed by a double quotation mark, then one backslash (\) is placed in the argv array for every pair of backslashes (\\), and the double quotation mark (") is interpreted as a string delimiter.

  • If an odd number of backslashes is followed by a double quotation mark, then one backslash (\) is placed in the argv array for every pair of backslashes (\\) and the double quotation mark is interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark (") to be placed in argv.

There is also a set of undocumented/non official rules (How Command Line Parameters Are Parsed)

  • Outside a double quoted block a " starts a double quoted block.
  • Inside a double quoted block a " followed by a different character (not another ") ends the double quoted block.
  • Inside a double quoted block a " followed immediately by another " (i.e. "") causes a single " to be added to the output, and the double quoted block continues.

The .Net argument parsing rules are just a derivation of those rules. If you need a different behaviour, then you should use the Environment.CommandLine property to retrieve the full command line string and write your own parsing code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.