究竟什么是“退出"?在 PowerShell 中? [英] What exactly is "exit" in PowerShell?

查看:56
本文介绍了究竟什么是“退出"?在 PowerShell 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以通过键入 exit 退出 PowerShell.到现在为止还挺好.但这究竟是什么?

You can exit PowerShell by typing exit. So far so good. But what exactly is this?

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、函数、脚本或程序.它留下了什么它到底是什么的问题.

So it's neither a cmdlet, function, script or program. It leaves the question what exactly it is.

不幸的是,这也意味着不能为 exit 创建别名:

This unfortunately also means that one can't create aliases to exit:

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

还有没有这样的命令而不是命令?

Are there any more such commands which are no commands?

预计到达时间: 仅供参考:我知道我可以简单地将它包装成一个函数.我的个人资料有线条

ETA: Just for reference: I know I can simply wrap it into a function. My profile has the lines

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

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

in them. My question was just to know what exactly this command is.

推荐答案

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

It's a reserved keyword (like return, filter, function, break).

参考

此外,根据 Bruce Payette 的 Powershell 的第 7.6.4 节在行动:

Also, as per Section 7.6.4 of Bruce Payette's Powershell in Action:

但是当您希望脚本从该脚本中定义的函数中退出时会发生什么?... 为简化此操作,Powershell 提供了 exit 关键字.

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 包装在函数中来做你想做的事情并不难:

Of course, as other have pointed out, it's not hard to do what you want by wrapping exit in a function:

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

这篇关于究竟什么是“退出"?在 PowerShell 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆