PowerShell 的调用运算符 (&) 语法和双引号 [英] PowerShell's call operator (&) syntax and double-quotes
问题描述
有人可以向我解释这个结果吗?多年来,我浪费了大量时间试图掌握 PowerShell 的调用命令语法,但是这……我什至无法猜测如何从输入中获得这个结果.
Can someone explain this result to me? I've wasted a lot of time over the years trying to master PowerShell's syntax for calling commands, but this...I can't even make a guess how to get this result from the input.
PS C:\Users\P> & echoargs " ""1"" 2 3 ""4 5 6"" 7 8 9"
Arg 0 is < 1 2 3 4>
Arg 1 is <5>
Arg 2 is <6 7 8 9>
布勒?
推荐答案
双引号字符串中的双引号是插入双引号的一种方式.echoargs.exe 的更新版本更清楚地显示了这一点,因为它向您显示了用于调用 exe 的命令行:
The doubled-up doubles quotes inside a double-quoted string is a way to insert a double quote. The updated version of echoargs.exe shows this a bit more clearly as it shows you the command line used to invoke the exe:
PS> echoargs " ""1"" 2 3 ""4 5 6"" 7 8 9"
Arg 0 is < 1 2 3 4>
Arg 1 is <5>
Arg 2 is <6 7 8 9>
Command line:
"C:\...\Modules\Pscx\Apps\EchoArgs.exe" " "1" 2 3 "4 5 6" 7 8 9"
如果你使用那个命令行(在它被 PowerShell 解析之后)你会在 CMD.exe 中得到相同的结果:
If you take that command line (after it has been parsed by PowerShell) you get the same result in CMD.exe:
CMD> EchoArgs.exe " "1" 2 3 "4 5 6" 7 8 9"
Arg 0 is < 1 2 3 4>
Arg 1 is <5>
Arg 2 is <6 7 8 9>
Command line:
C:\...\Modules\Pscx\Apps\EchoArgs.exe " "1" 2 3 "4 5 6" 7 8 9"
至于为什么 .NET 或 C++ 启动代码以这种方式解析命令行,我不完全确定.这个 MSDN 主题 对此进行了一些介绍如果您查看主题底部的示例,您会看到一些同样奇怪的解析行为,例如a\\\bd"ef"gh
给出 a\\\b
、de fg
和 h
.
As to why .NET or the C++ startup code parses the command line that way, I'm not entirely sure. This MSDN topic covers it a bit and if you look at the examples at the bottom of the topic, you will see some equally weird parsing behavior e.g. a\\\b d"e f"g h
gives a\\\b
, de fg
and h
.
这篇关于PowerShell 的调用运算符 (&) 语法和双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!