Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command line arguments to a shell alias? [duplicate]

Tags:

alias

shell

How do I pass the command line arguments to an alias? Here is a sample:

 alias mkcd='mkdir $1; cd $1;' 

But in this case the $xx is getting translated at the alias creating time and not at runtime. I have, however, created a workaround using a shell function (after googling a little) like below:

 function mkcd(){   mkdir $1   cd $1 } 

Just wanted to know if there is a way to make aliases that accept CL parameters.
BTW - I use 'bash' as my default shell.

like image 508
Vini Avatar asked Jun 02 '09 19:06

Vini


People also ask

How do you pass arguments to alias commands?

You can replace $@ with $1 if you only want the first argument. This creates a temporary function f , which is passed the arguments. Alias arguments are only passed at the end. Note that f is called at the very end of the alias.

Can alias take arguments bash?

Bash users need to understand that alias cannot take arguments and parameters. But we can use functions to take arguments and parameters while using alias commands.


1 Answers

Just to reiterate what has been posted for other shells, in Bash the following works:

alias blah='function _blah(){ echo "First: $1"; echo "Second: $2"; };_blah' 

Running the following:

blah one two 

Gives the output below:

First: one Second: two 
like image 71
Thomas Bratt Avatar answered Dec 05 '22 23:12

Thomas Bratt