[英]Powershell: What's the difference between Alias and Function?
Im setting up my powershell profile to create aliases of commonly used commands.我正在设置我的 powershell 配置文件以创建常用命令的别名。 On Microsoft's documentation it says, if I want to make an alias for a command with parameters, I should make the value of the alias a Function that does that.
在 Microsoft 的文档中,它说,如果我想为带有参数的命令创建别名,我应该将别名的值设为 Function 来做到这一点。
.
. However, when I type the name of the function in the command line it works just as well as an alias.
但是,当我在命令行中键入 function 的名称时,它的工作原理与别名一样。
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换句话说,在上图中,如果我键入
CD32
,它的行为与我在命令行中键入Go
的行为相同
So my question is: Why do I use aliases pointing to functions when I could just have a function?所以我的问题是:为什么我只能使用 function 时使用指向函数的别名? Are there feature differences between the two?
两者之间有功能差异吗?
An alias in PowerShell allows you to define an alternative name for another command . PowerShell 中的别名允许您为另一个命令定义替代名称。
Unlike in POSIX-compatible shells such as bash
, you cannot include pass-through arguments in its definition - you need a function for that .与 POSIX 兼容的 shell 不同,例如
bash
,您不能在其定义中包含直通 arguments - 您需要一个function 。
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 its Get-Content
cmdlet.例如,PowerShell 的
Get-Content
cmdlet 有一个内置的gc
别名。 PowerShell even recommends a naming convention for aliases, based on official short alias prefixes for its approved verbs , such as the g
for the Get
verb in the given example. PowerShell 甚至建议别名的命名约定,基于其已批准动词的官方短别名前缀,例如给定示例中
Get
动词的g
。
Another, problematic use is to define aliases named for a different shell's commands;另一个有问题的用途是定义为不同 shell 的命令命名的别名。 for instance, PowerShell has a built in
dir
alias for its Get-ChildItem
, named for cmd.exe
's (Command Prompt's) internal dir
command.例如, PowerShell 为其
Get-ChildItem
有一个内置的dir
别名,以cmd.exe
的(命令提示符的)内部dir
命令命名。 While that may be somewhat helpful while transitioning from cmd.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.虽然这在从
cmd.exe
转换时可能会有所帮助,但它仅适用于非常简单的调用,并且由于 PowerShell 的命令行语法和参数名称完全不同,很快就会出现问题。
Another, unproblematic use is to define an alias for an external executable whose directory isn't listed in the path ( $env:PATH
);另一个没有问题的用途是为其目录未列在路径 (
$env:PATH
) 中的外部可执行文件定义别名; eg, if you want to execute c:\path\to\foo.exe
as just foo
without having to add c:\path\to
to $env:PATH
, you can use Set-Alias foo c:\path\to\foo.exe
. eg, if you want to execute
c:\path\to\foo.exe
as just foo
without having to add c:\path\to
to $env:PATH
, you can use Set-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.与 POSIX 兼容的 shell(例如
bash
)不同,别名(总是)可用于脚本( *.ps1
文件),但出于稳健性和长期稳定性的考虑,不鼓励在脚本中使用别名。
A function , as is to be expected, is a named unit of code that can accept arguments and can perform arbitrary operations .正如预期的那样, function是一个命名的代码单元,可以接受 arguments 并且可以执行任意操作。
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):至于为function 定义别名是否有意义,如果您的命令的实现需要 function (由于需要的不仅仅是简单的名称映射):
If all you need is one (short) name for your command, you can define your function directly with that name - no alias needed.如果您只需要一个(短)命令名称,您可以使用该名称直接定义 function - 不需要别名。
By contrast, if your function needs a long name, especially an advanced (cmdlet-like) function that adheres to PowerShell's verb-noun naming convention (eg, Invoke-Foo
), and you also want a short name for interactive convenience (eg, foo
), you'll have to also define an alias for that function with that short name (eg, Set-Alias foo Invoke-Foo
).相比之下,如果您的 function 需要一个长名称,尤其是一个高级(类似于 cmdlet)的 function ,它遵循 PowerShell 的动词-名词命名约定(例如,
Invoke-Foo
),并且您还需要一个短名称以方便交互(例如, foo
),您还必须使用该短名称为 function 定义一个别名(例如, Set-Alias foo Invoke-Foo
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.