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 an alias 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?
Best Answer
Aliases don't take arguments. With an alias like
alias foo='bar $1'
, the$1
will be expanded by the shell to the shell's first argument (which is likely nothing) when the alias is run.So: Use functions, instead.
num=${1:-5}
uses the first argument, with a default value of 5 if it isn't provided.Then you can do:
Or, if you change the options you used slightly:
Then you can pass additional
-n
options:If multiple
-n
options are specified fortail
, only the last is used.