This question already has an answer here:

I want to know if i can pass an argument with an alias command.

for example:

alias d="dmesg|grep -iw usb|tail -5" 

Now d will print the last 5 lines. if i want to use d to print a different number of lines, i have to make change in the alias command declaration of d again.

Is there any way i can modify the declaration of alias command so that i don't have to retype the declaration to change the number of lines. Like incorporating passing the number of lines as an argument while declaring alias for d or is there some other method to solve this?

marked as duplicate by jasonwryan, Stephen Kitt, Michael Homer, Anthon, cuonglm May 21 '15 at 11:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • which shell are you using? – Skaperen May 21 '15 at 10:22

Can an argument be passed through the “alias”

No. An alias is a simple string-replace.

Use functions.

   dmesg|grep -iw usb|tail -"$1" 

In the case - yes, you can as exception, because you argument is the last in line:

alias d='dmesg | grep -iw usb | tail'
d -5

should work.


Which shell are you using ? In zsh arguments can be passed something like this -

d () {
    dmesg|grep -iw usb|tail -$1

alias would always just substitute the short string with the long string.

Not the answer you're looking for? Browse other questions tagged or ask your own question.