bash 如何使用参数创建bash别名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11466419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create bash alias with argument?
提问by RanRag
I normally use ps -elf | grep proceesname
to get a detailed description of the process named processname
. I think that I have to write too much for this.
我通常ps -elf | grep proceesname
用来获取名为processname
. 我想我必须为此写太多。
Now what i was thinking is to create a bash alias like
现在我在想的是创建一个 bash 别名,如
alias lsps='ps -elf | grep '
which will give the above detailed description only by using lsps processname
.
这将仅通过使用lsps processname
.
So, my question is how do I create a bash alias which accepts an argument.
所以,我的问题是如何创建一个接受参数的 bash 别名。
PS: I know I can write a shell script for the above task but I was just wondering how to do it with bash alias.
PS:我知道我可以为上述任务编写一个 shell 脚本,但我只是想知道如何使用 bash 别名来完成它。
回答by Igor Chubin
Very simple;
很简单;
alias lsps='ps -elf | grep'
Command line arguments will be added automatically to the end of the alias:
命令行参数将自动添加到别名的末尾:
lsps arg1 arg2 arg3 => converted to => ps -elf | grep arg1 arg2 arg3
That works only when you want to add arguments to the end of alias.
仅当您想在别名末尾添加参数时才有效。
If you want to get arguments of the alias inside of the expanded command line you must use functions:
如果要在展开的命令行中获取别名的参数,则必须使用函数:
For example:
例如:
lsps()
{
ps -elf | grep "" | grep -v grep
}
Functions as well as aliases can be saved in your ~/.bashrc
file )or a file that is included from it):
函数和别名可以保存在您的~/.bashrc
文件中)或其中包含的文件中):
$ cat /tmp/.bash_aliases
lsps()
{
ps -elf | grep "" | grep -v grep
}
$ . /tmp/.bash_aliases
$
回答by Ramazan Polat
Use this:
用这个:
alias lsps='ps -elf | grep'
Then you can issue this:
然后你可以发出这个:
lsps processname