如何将命令行参数传递给 shell 别名? [英] How to pass command line arguments to a shell alias?

查看:36
本文介绍了如何将命令行参数传递给 shell 别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将命令行参数传递给别名?这是一个示例:

<前>别名 mkcd='mkdir $1;cd $1;'

但在这种情况下,$xx 在别名创建时而不是在运行时被翻译.但是,我已经使用如下所示的 shell 函数(在谷歌搜索之后)创建了一个解决方法:

<前>函数 mkcd(){目录 $1光盘 $1}

只是想知道是否有办法制作接受 CL 参数的别名.
顺便说一句 - 我使用bash"作为我的默认 shell.

解决方案

您找到了方法:创建函数而不是别名.C shell 有一个机制来处理别名的参数,但 bash 和 Korn shell 没有,因为函数机制更灵活并且提供相同的功能.

How do I pass the command line arguments to an alias? Here is a sample:

alias mkcd='mkdir $1; cd $1;'

But in this case the $xx is getting translated at the alias creating time and not at runtime. I have, however, created a workaround using a shell function (after googling a little) like below:

function mkcd(){
  mkdir $1
  cd $1
}

Just wanted to know if there is a way to make aliases that accept CL parameters.
BTW - I use 'bash' as my default shell.

解决方案

You found the way: create a function instead of an alias. The C shell has a mechanism for doing arguments to aliases, but bash and the Korn shell don't, because the function mechanism is more flexible and offers the same capability.

这篇关于如何将命令行参数传递给 shell 别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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