如何将命令行参数传递给Shell别名? [英] How to pass command line arguments to a shell alias?
问题描述
如何将命令行参数传递给别名?下面是一个示例:
How do I pass the command line arguments to an alias? Here is a sample:
alias mkcd='mkdir $1; cd $1;'
但是在这种情况下,$ xx在别名创建时而不是在运行时转换。但是,我已经使用如下shell函数创建了一种变通方法(在谷歌搜索了一下之后):
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
}
只是想知道是否有办法使别名接受CL参数。
BTW-我使用'bash'作为默认外壳。
Just wanted to know if there is a way to make aliases that accept CL parameters.
BTW - I use 'bash' as my default shell.
推荐答案
方式:创建函数而不是别名。 C shell具有一种对别名进行参数处理的机制,而bash和Korn 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屋!