Tuesday, 19 July 2016

bash - How do I create an alias where the arguments go in the middle?





I'm trying to define an alias where the arguments are inserted in the middle, instead of appended to the end.



I tried defining it like this:



alias grep_logs="grep $1 */log/*.log"



where $1 is the first argument to grep_logs, such that:



grep_logs foo


would execute the following command:



grep foo */log/*.log



but instead, it runs the command:



grep foo */log/*.log foo


which results in the error:



grep: foo: No such file or directory



Is it possible to do this using an alias or do I need to define a function?


Answer



Try defining a function in ~/.profile.



function greplogs(){
grep "$1" */logs/*.log
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...