如何创建别名的bash用争论? [英] How to create bash alias with argument?

查看:96
本文介绍了如何创建别名的bash用争论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用 PS -elf | grep的proceesname 来获得命名的过程 processname 的详细说明。我认为我必须写太多了这一点。

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.

现在我当时的想法是创建一个bash别名像

Now what i was thinking is to create a bash alias like

alias lsps='ps -elf | grep $1'

这只会用的LSP p​​rocessname 给上面的详细描述。

所以,我的问题是如何创建它接受一个参数一个bash别名。

So, my question is how do I create a bash alias which accepts an argument.

PS:我知道我可以写一个shell脚本为上述任务,但我只是想知道如何使用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.

推荐答案

很简单;

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:

例如:

lsps()
{
    ps -elf | grep "$1" | grep -v grep
}

功能以及别名可以在〜/ .bashrc中文件被保存),或者从它包含的文件):

Functions as well as aliases can be saved in your ~/.bashrc file )or a file that is included from it):

$ cat /tmp/.bash_aliases
lsps()
{
    ps -elf | grep "$1" | grep -v grep
}

$ . /tmp/.bash_aliases
$

这篇关于如何创建别名的bash用争论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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