如何在 Powershell 中为特定命令设置别名? [英] How do I set an alias for a specific command in Powershell?

查看:102
本文介绍了如何在 Powershell 中为特定命令设置别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常输入一个命令:java -jar foo --param1 --param2, --param-n.如何创建命令别名来运行此命令,如下所示:

There is a command that I type a lot: java -jar foo --param1 --param2, --param-n. How can I create a command alias to run this command like so:

launch_foo --param3

launch_foo 在哪里翻译成完整的命令?

where launch_foo is translated to the full command?

推荐答案

您必须创建一个函数.幸运的是,创建一个别名类型"的函数并不困难:

You have to create a function. Fortunately, creating an "alias-type" function isn't difficult:

Function launch_foo { java -jar foo --param1 --param2 --param-n $args }

您现在可以以 launch_foolaunch_foo --param3 的形式调用该函数.请注意,如果您不传入任何其他参数,$args 将为 $null 并且不会影响函数中指定的默认参数.

You can now invoke the function either as launch_foo or as launch_foo --param3. Note that if you don't pass in any additional arguments, $args will be $null and will have no effect on the default arguments specified in the function.

现在,Powershell 中有别名的概念,但别名是 cmdlet 或命令的备用名称,您不能向别名定义添加参数.您可以使用别名作为替代的可执行文件或 cmdlet 名称.一些别名示例:

Now, there is the concept of aliases in Powershell but aliases are alternate names for cmdlets or commands, you can't add arguments to the alias definition. You would use an alias as an alternate executable or cmdlet name. Some examples of aliases:

Set-Alias v vim
Set-Alias list Get-ChildItem
Set-Alias 2json ConvertTo-Json

v 将运行 vimlist 将运行 Get-ChildItem2json 将运行 ConvertTo-Json.

v would run vim, list would run Get-ChildItem, and 2json would run ConvertTo-Json.

这篇关于如何在 Powershell 中为特定命令设置别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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