在中间用paramater创建别名

我想为grep创建一个别名,如下所示:

grep argX ~/myfile 

其中argX是一个参数,myfile始终是相同的。 我怎样才能做到这一点?

别名不支持参数,但您可以编写一个小脚本并将其命名为“filegrep”

 #!/bin/bash grep $1 /home/youruser/myfile 

将脚本复制到/usr/bin ,您可以在控制台中使用filegrep argX运行它。

别名不支持位置参数,因此您需要创建一个函数(在~/.profile )并为该函数设置别名。

 function grepMe(){ grep "$1" ~/myfile } 

然后别名..

 alias grepAlias="grepMe" 

在这里,我找到了一个不使用函数的替代

 alias grepAlias='bash -xc '\''grep $0 ~/myfile'\''' 

例如使用Silver Searcher :

 alias superlocate='bash -xc '\''ag -g $0 --hidden'\'' 2>/dev/null' 
Interesting Posts