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

标签 powershell

您可以通过键入 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/

相关文章:

c# - 使用SwitchParameter的PowerShell自定义Cmdlet

winforms - 哈希表内的 WinForm

arrays - Powershell中的奇怪脚本问题

visual-studio - Visual Studio 2019 CE Powershell 项目

datetime - 具有特定日期/时间范围的PowerShell脚本

Powershell 脚本为用户创建主文件夹并设置权限

powershell - 用于启用/禁用 TCP 端口的 Windows 10 脚本

powershell - 是否可以使用 powershell 从 Azure 市场创建 Umbraco 站点?

powershell - 如何使用 Sonar-Runner.bat 动态设置 ProjectVersion

Java Process.waitFor() 和 Readline 挂起