gcm exit Get-Command : The term 'exit' is-6ren">
gpt4 book ai didi

powershell - PowerShell 中的 "exit"到底是什么?

转载 作者:行者123 更新时间:2023-12-03 05:35:50 24 4
gpt4 key购买 nike

您可以通过键入 exit 退出 PowerShell。到目前为止,一切都很好。但这到底是什么?

PS Home:\> gcm exit
Get-Command : The term 'exit' is not recognized as the name of a cmdlet, function, script file, or operable program. Ch
eck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:4
+ gcm <<<< exit
+ CategoryInfo : ObjectNotFound: (exit:String) [Get-Command], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

所以它既不是 cmdlet、函数、脚本也不是程序。它留下了问题它到底是什么

不幸的是,这也意味着无法创建别名来退出:

PS Home:\> New-Alias ^D exit
PS Home:\> ^D
Cannot resolve alias '♦' because it refers to term 'exit', which is not recognized as a cmdlet, function, operable prog
ram, or script file. Verify the term and try again.
At line:1 char:2
+ ♦ <<<<
+ CategoryInfo : ObjectNotFound: (♦:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : AliasNotResolvedException

还有更多这样的非命令命令吗?

预计到达时间:仅供引用:我知道我可以简单地将其包装到一个函数中。我的个人资料中有这样的行

# exit with Ctrl+D
iex "function $([char]4) { exit }"

在他们之中。我的问题只是想知道这个命令到底是什么。

最佳答案

它是一个保留关键字(如 return、filter、function、break)。

Reference

此外,根据 Bruce Payette 的 Powershell in Action 第 7.6.4 节:

But what happens when you want a script to exit from within a function defined in that script? ... To make this easier, Powershell has the exit keyword.

当然,正如其他人指出的那样,通过将 exit 包装在函数中来完成您想要的操作并不难:

PS C:\> function ex{exit}
PS C:\> new-alias ^D ex

关于powershell - PowerShell 中的 "exit"到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1275090/

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