Powershell 中 $ 的含义是什么?
?
编辑:TechNet answers同义反复,没有解释“成功”或“失败”的含义。
$?
Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.
我猜$?会简单地测试 $LastExitCode 是否为 0,但我发现了 counter example哪里$?为 False 但 $LastExitCode 为 True。
最佳答案
如果最后一个命令成功,则返回 true
,否则返回 false
。
但是,有许多警告和非显而易见的行为(例如“成功”的确切含义)。我强烈推荐阅读this article以获得更全面的治疗。
例如,考虑调用 Get-ChildItem。
PS> Get-ChildItem
PS> $?
True
$?当调用 Get-ChildItem 成功时,将返回 True
。
但是,如果您在不存在的目录上调用 Get-ChildItem,则会返回错误。
PS> Get-ChildItem \Some\Directory\Which\Does\Not\Exist
Get-ChildItem : Cannot find path 'C:\Some\Directory\Which\Does\Not\Exist' because it does not exist.
PS> $?
False
$?此处将返回 False
,因为上一个命令未成功。
关于powershell - Powershell 中的 `$?` 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10634115/