在 Powershell Invoke-Expression 中转义分散变量
我正在尝试从 Powershell 调用另一个应用程序(超越比较),该应用程序在典型的命令行中需要 @:
C:\deploy>bcompare @static.diff
我找到了 Powershell 的 Invoke-Expression,但是当我尝试以下操作时,它给了我一个错误:
PS C:\deploy>Invoke-Expression "bcompare @static.diff"
Invoke-Expression : Cannot expand the splatted variable '@static'. Splatted variables
cannot be used as part of a property or array expression. Assign the result of the
expression to a temporary variable then splat the temporary variable instead.
At line:1 char:18
+ Invoke-Expression <<<< "bcompare @static.diff"
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : NoPropertiesInSplatting,Microsoft.PowerShell.Comands.InvokeExpressionCommand
我不能让@在这里正确转义。我尝试过`、@@,将命令的一部分放入临时变量中,但没有一个成功。
I'm trying to invoke another application (Beyond Compare) from Powershell which requires an @ in the typical command-line:
C:\deploy>bcompare @static.diff
I've found Powershell's Invoke-Expression, but when I try the following it gives me an error:
PS C:\deploy>Invoke-Expression "bcompare @static.diff"
Invoke-Expression : Cannot expand the splatted variable '@static'. Splatted variables
cannot be used as part of a property or array expression. Assign the result of the
expression to a temporary variable then splat the temporary variable instead.
At line:1 char:18
+ Invoke-Expression <<<< "bcompare @static.diff"
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : NoPropertiesInSplatting,Microsoft.PowerShell.Comands.InvokeExpressionCommand
I can't get the @ to escape properly here. I've tried the `, @@, putting parts of the command in a temporary variable, but none of them did the trick.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果有疑问,请将其放入字符串中:-)
If in doubt, put it into a string :-)
你需要双重逃避,因为你正在经历两个层次的解释。只有一个 ` 不起作用,因为它在字符串创建过程中被解析。
或者正如乔伊所说。
You need to double escape, because you are going through two levels of interpretation. Only one ` will not work because it get parsed during the string creation.
Or as Joey said.
当我遇到同样的问题时,我使用反引号来按字面解释@符号。我也想使用双引号进行变量处理:
When I ran into the same problem, I used a backtick to make the @-sign interpreted literally. I wanted to use double-quotes for variable handling as well:
我在 Windows 命令行上的 npm install 上遇到错误
splatting 运算符“@”不能用于引用表达式中的变量。
但单引号解决了这个问题
注意 - NPM 官方网站没有提到单引号。
I was getting an error on npm install on the windows command line
The splatting operator '@' cannot be used to reference variables in an expression.
But single quotes resolved the problem
NOTE - Official NPM site doesn't mention a single quotes.