Powershell:别名和功能之间有什么区别? [英] Powershell: What's the difference between Alias and Function?
问题描述
我设置我的Powershell配置文件以创建常用命令的别名..但是,当我在命令行中键入函数的名称时,它的功能与别名一样.
换句话说,在上图中,如果我键入 CD32
,其行为与在命令行中键入 Go
的行为相同.
所以我的问题是:当我只有一个函数时,为什么要使用指向函数的别名?两者之间是否存在功能差异?
-
PowerShell中的 别名 允许您为另一个命令定义 另一个名称 .
-
与POSIX兼容的shell(例如
bash
)不同,您不能在其定义中包括传递参数-您需要功能.. -
典型用例是为交互式调用的便捷性 定义一个 short 替代名称;例如,PowerShell在其
Get-Content
cmdlet中具有内置的gc
别名.PowerShell甚至根据其此答案.
-
对于 为函数定义别名 是否有意义(如果命令的实现需要函数(由于不仅需要简单的名称映射,还需要更多功能)):
-
如果您所需要的只是命令的一个(简称),则可以使用该名称直接定义函数 -无需别名.>
-
相比之下,如果您的函数需要一个 long 名称,尤其是一个 解决方案
An alias in PowerShell allows you to define an alternative name for another command.
Unlike in POSIX-compatible shells such as
bash
, you cannot include pass-through arguments in its definition - you need a function for that.The typical use case is to define a short alternative name for convenience of interactive invocation; for instance, PowerShell has a built in
gc
alias for itsGet-Content
cmdlet. PowerShell even recommends a naming convention for aliases, based on official short alias prefixes for its approved verbs, such as theg
for theGet
verb in the given example.Another, problematic use is to define aliases named for a different shell's commands; for instance, PowerShell has a built in
dir
alias for itsGet-ChildItem
, named forcmd.exe
's (Command Prompt's) internaldir
command. While that may be somewhat helpful while transitioning fromcmd.exe
, it only works in very simple invocations, and quickly becomes problematic due to PowerShell's fundamentally different command-line syntax and differing parameter names.Another, unproblematic use is to define an alias for an external executable whose directory isn't listed in the path (
$env:PATH
); e.g., if you want to executec:\path\to\foo.exe
as justfoo
without having to addc:\path\to
to$env:PATH
, you can useSet-Alias foo c:\path\to\foo.exe
.Unlike in POSIX-compatible shells such as
bash
, aliases are (invariably) usable in scripts (*.ps1
files), but their use in scripts is discouraged in the interest of robustness and long-term stability.
A function, as is to be expected, is a named unit of code that can accept arguments and can perform arbitrary operations.
- A function is what you need to use if you want to wrap existing commands, by hard-coding pass-through arguments and / or providing custom logic around the invocation of the wrapped command - see this answer.
As for whether it makes sense to define an alias for a function, if implementation of your command requires a function (due to requiring more than just a simple name mapping):
If all you need is one (short) name for your command, you can define your function directly with that name - no alias needed.
By contrast, if your function needs a long name, especially an advanced (cmdlet-like) function that adheres to PowerShell's verb-noun naming convention (e.g.,
Invoke-Foo
), and you also want a short name for interactive convenience (e.g.,foo
), you'll have to also define an alias for that function with that short name (e.g.,Set-Alias foo Invoke-Foo
).
这篇关于Powershell:别名和功能之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!