我通常使用ps -elf | grep proceesname
来获取名为processname
. 我认为我必须为此写太多。
现在我在想的是创建一个 bash 别名
alias lsps='ps -elf | grep $1'
这将仅通过使用来给出上述详细描述lsps processname
。
所以,我的问题是如何创建一个接受参数的 bash 别名。
PS:我知道我可以为上述任务编写一个 shell 脚本,但我只是想知道如何使用 bash 别名来完成它。
我通常使用ps -elf | grep proceesname
来获取名为processname
. 我认为我必须为此写太多。
现在我在想的是创建一个 bash 别名
alias lsps='ps -elf | grep $1'
这将仅通过使用来给出上述详细描述lsps processname
。
所以,我的问题是如何创建一个接受参数的 bash 别名。
PS:我知道我可以为上述任务编写一个 shell 脚本,但我只是想知道如何使用 bash 别名来完成它。
很简单;
alias lsps='ps -elf | grep'
命令行参数将自动添加到别名的末尾:
lsps arg1 arg2 arg3 => converted to => ps -elf | grep arg1 arg2 arg3
仅当您想在别名末尾添加参数时才有效。
如果要在扩展命令行中获取别名的参数,则必须使用函数:
例如:
lsps()
{
ps -elf | grep "$1" | grep -v grep
}
函数和别名可以保存在您的~/.bashrc
文件中)或其中包含的文件):
$ cat /tmp/.bash_aliases
lsps()
{
ps -elf | grep "$1" | grep -v grep
}
$ . /tmp/.bash_aliases
$
用这个:
alias lsps='ps -elf | grep'
然后你可以发出这个:
lsps processname