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

sferik picture sferik · Aug 24, 2011 · Viewed 15.1k times · Source

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

Jack picture Jack · Aug 24, 2011

Try defining a function in ~/.profile.

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