PowerShell:函数插值
我是 PowerShell 初学者。应该使用 $()
让 Write-Host 来评估此函数的原因是什么?
我从文档中找不到原因。
PS C:\Temp> Write-Host [math]::round($diff.TotalMinutes, 2)
[math]::round 751681,102679735 2
___________________________________________________________________________________________________________
PS C:\Temp> Write-Host $([math]::round($diff.TotalMinutes, 2))
751681,1
I'm a PowerShell beginner. What is the reason that one should use $()
to get Write-Host to evaluate this function?
I could not find a reason for this from the documentation.
PS C:\Temp> Write-Host [math]::round($diff.TotalMinutes, 2)
[math]::round 751681,102679735 2
___________________________________________________________________________________________________________
PS C:\Temp> Write-Host $([math]::round($diff.TotalMinutes, 2))
751681,1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以删除美元符号并仅使用
解析器需要的括号,以便首先计算表达式,然后将其绑定到参数
-Object
。当解析器期望字符串将是表达式以及何时将其视为字符串并将其传递给命令而不进行评估时,有一些规则。更多信息请参阅PowerShell 实战。
如果有多个表达式,则需要美元符号,以
;
分隔You can remove the dollar sign and use just
The brackets are needed for the parser so that first the expression is evaluated and then bound to parameter
-Object
.There are some rules when parser expects that the string will be expression and when it treats it as a string and passes it to the command without evaluating. More info can be found in PowerShell in Action.
Dollar sign is needed if there are more expressions separated by
;