Powershell:别名和功能之间有什么区别? [英] Powershell: What's the difference between Alias and Function?

查看:58
本文介绍了Powershell:别名和功能之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置我的Powershell配置文件以创建常用命令的别名..但是,当我在命令行中键入函数的名称时,它的功能与别名一样.

换句话说,在上图中,如果我键入 CD32 ,其行为与在命令行中键入 Go 的行为相同.

所以我的问题是:当我只有一个函数时,为什么要使用指向函数的别名?两者之间是否存在功能差异?

解决方案

  • PowerShell中的 别名 允许您为另一个命令定义 另一个名称 .

    • 与POSIX兼容的shell(例如 bash )不同,不能在其定义中包括传递参数-您需要功能..

    • 典型用例是为交互式调用的便捷性 定义一个 short 替代名称;例如,PowerShell在其 Get-Content cmdlet中具有内置的 gc 别名.PowerShell甚至根据其此答案.


对于 为函数定义别名 是否有意义(如果命令的实现需要函数(由于不仅需要简单的名称映射,还需要更多功能)):

  • 如果您所需要的只是命令的一个(简称),则可以使用该名称直接定义函数 -无需别名.

  • 相比之下,如果您的函数需要一个 long 名称,尤其是一个 解决方案


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屋!

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