为什么在CMD中尾随反斜杠后加引号的行为如此奇怪? [英] Why does trailing backslash followed by quote behave so strangely in cmd?

查看:225
本文介绍了为什么在CMD中尾随反斜杠后加引号的行为如此奇怪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此程序( cs.exe ):

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

然后运行:

> 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表现出类似的行为,但单引号和双引号没有区别。

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

我如何理解这种行为?在cmd中评估反斜杠的基本规则是什么,以便可以始终如一地解释?

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

推荐答案

因为您没有调用内部 cmd 命令,但是调用可执行文件,此行为不是由 cmd 引起的,而是命令行参数解析器例程引起的。在Windows中,程序不会收到参数的集合/数组/集合,而是一个包含所有参数的字符串,并且每个程序都会对该字符串进行标记化以获得每个元素。通常,这是通过编译器包含的例程完成的,该例程隐藏了此操作,并向代码提供了一种更简单的方法来处理参数。

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.

C命令行参数解析器指出



  • 参数由空格分隔,空格可以是空格或制表符。

  • 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.

如果偶数个反斜杠后跟一个双引号,则每对
对一个反斜杠( \ )放在argv数组中反斜杠( \\ ),双引号( )为
解释为

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.

如果有奇数个反斜杠后跟双引号,则使用一个反斜杠( \ )每对
对反斜杠( \\ )放在argv数组中,双引号被解释为
作为其余背的逃逸顺序睫毛,导致将文字
双引号( )放在argv中。

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.

.Net 参数解析规则是只是这些规则的衍生。如果您需要其他行为,则应使用 Environment.CommandLine 属性可检索完整的命令行字符串并编写自己的解析代码。

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.

这篇关于为什么在CMD中尾随反斜杠后加引号的行为如此奇怪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆