gpt4 book ai didi

powershell - () 和 $() 的区别

转载 作者:行者123 更新时间:2023-12-04 17:31:04 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What is the difference between $a = $(Read-Host) and $a = (Read-Host)?

(2 个回答)


3年前关闭。




有什么区别

Write-Host (Get-Date) # just paren


Write-Host $(Get-Date) # dollar-paren

括号内的内容可以是任何内容,只需举一个简单的例子。两者之间有什么区别吗?

我认为自己对 PS 有相当的经验,但正是这些小事情让我感到困扰,尤其是在代码审查等过程中。有没有人遇到过“这就是语言的工作原理”的良好来源,其中有足够的细节来得出这些问题的答案?

最佳答案

子表达式 ( $(...) ) 包含一个 StatementBlockAst .可以花任意号码 语句、含义关键字( ifforeach 等)、管道、命令等。解析类似于命名块的内部,如 begin/process/end .

括号表达式 ( (...) ) 可以包含 单例 ExpressionAst这是 AST 的一个有限子集。语句和表达式之间最显着的区别是不解析关键字。

$(if ($true) { 'It worked!' })
# It worked!

(if ($true) { 'It worked!' })
# if : The term 'if' is not recognized as the name of a cmdlet, function,
# script file, or operable program. Check the spelling of the name, or
# if a path was included, verify that the path is correct and try again.
# At line:1 char:2
# + (if ($true) { 'It worked' })
# + ~~
# + CategoryInfo : ObjectNotFound: (if:String) [], CommandNotFoundException
# + FullyQualifiedErrorId : CommandNotFoundException

同样正如其他人所指出的,子表达式将在双引号字符串中展开。

关于powershell - () 和 $() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47274532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com