Powershell:别名和函数有什么区别? [英] Powershell: What's the difference between Alias and Function?
问题描述
我正在设置我的 powershell 配置文件以创建常用命令的别名..但是,当我在命令行中键入函数的名称时,它的作用与别名一样好.
换句话说,在上图中,如果我输入 CD32
,它的行为与我在命令行中输入 Go
的行为相同
所以我的问题是:当我可以拥有一个函数时,为什么要使用指向函数的别名?两者之间有功能差异吗?
PowerShell 中的别名允许您为另一个命令定义替代名称.
与 POSIX 兼容的 shell(例如
bash
)不同,您不能在其定义中包含传递参数 - 您需要一个 作用.典型用例是为交互式调用的便利定义一个短替代名称;例如,PowerShell 为其
Get-Content
cmdlet 提供了一个内置的gc
别名.PowerShell 甚至推荐了别名的命名约定,基于其 批准的动词,例如给定示例中Get
动词的g
.另一个问题用法是定义为不同shell的命令命名的别名;例如,PowerShell 为其
Get-ChildItem
提供了一个内置的dir
别名,以cmd.exe
的(命令提示符的)内部dir
命令.虽然这在从cmd.exe
转换时可能会有所帮助,但它仅适用于非常简单的调用,并且由于 PowerShell 根本不同的命令行语法和不同的参数名称,它很快就会出现问题.另一个没有问题的用途是为外部可执行文件定义别名,该文件的目录未在路径 (
$env:PATH
) 中列出;例如,如果你想像foo
一样执行c:path ofoo.exe
而不必添加c:path o
> 到$env:PATH
,可以使用Set-Alias foo c:path ofoo.exe
.与 POSIX 兼容的 shell 不同,例如
bash
,别名 are(总是)在 scripts 中可用(*.ps1
文件),但为了健壮性和长期稳定性,不鼓励在脚本中使用它们.
函数,正如所料,是一个命名的代码单元,可以接受参数并可以执行任意操作.
- 如果您想包装现有命令,则需要使用函数,通过硬编码传递参数和/或围绕调用提供自定义逻辑包装的命令 - 请参阅此答案.
至于 为函数定义别名是否有意义,如果您的命令的实现需要一个函数(因为需要的不仅仅是一个简单的名称映射):
如果您只需要一个(短)名称,您可以直接使用该名称定义您的函数 - 不需要别名.>
相比之下,如果您的函数需要长名称,尤其是高级(类 cmdlet)函数,遵循 PowerShell 的动词-名词命名约定(例如,
Invoke-Foo
),并且您还需要一个 short 名称以方便交互(例如foo
),您还必须使用该短名称为该函数定义一个别名(例如,Set-Alias foo Invoke-Foo
).
Im setting up my powershell profile to create aliases of commonly used commands. . However, when I type the name of the function in the command line it works just as well as an alias.
In other words, in the above picture, if I typed CD32
it would behave the same as if I typed Go
in the command line
So my question is: Why do I use aliases pointing to functions when I could just have a function? Are there feature differences between the two?
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 ofoo.exe
as justfoo
without having to addc:path o
to$env:PATH
, you can useSet-Alias foo c:path ofoo.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屋!