我正在测试一个函数,并且在尝试解决我return
遇到的问题,在PowerShell 5.1和PwSh 7.1中偶然发现,该return
cmdlet在一组中似乎不起作用:
PS C:\Users\Neko> return "test"
test
PS C:\Users\Neko> (return "test")
return : The term 'return' 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
+ (return "test")
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (return:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
如果我return
加入一个小组,PowerShell似乎无法识别它。
据我所知,回报打印到控制台,并没有“产出”每说一样Write-Host
,
PS C:\Users\Neko> $test = return "test"
test
PS C:\Users\Neko> $test
PS C:\Users\Neko> $test = Write-Host "test"
test
PS C:\Users\Neko> $test
PS C:\Users\Neko>
但我无法理解为什么return
在分组表达式中出现会导致此类问题。
为什么return
在组中使用会导致奇怪的错误,我该如何解决?
在子表达式中不会发生这种情况,
PS C:\Users\Neko> $(return "test")
test
@Neko Musume-根据您的要求
因为按照关于该主题的MS Docs ...
...只能按定义使用。意思是“退出当前范围,可以是函数,脚本或脚本块。”,因此,在尝试使用parens时,不符合该条件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句