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.
An empty alias will execute its args:
You found the way: create a function instead of an alias. The C shell has a mechanism for doing arguments to aliases, but bash and the Korn shell don't, because the function mechanism is more flexible and offers the same capability.
You cannot in ksh, but you can in csh.
In ksh, function is the way to go. But if you really really wanted to use alias:
You may also find this command useful:
mkdir dirname && cd $_
where dirname is the name of the directory you want to create
Just to reiterate what has been posted for other shells, in Bash the following works:
Running the following:
Gives the output below:
The easiest way, is to use function not alias. you can still call a function at any time from the cli. In bash, you can just add function name() { command } it loads the same as an alias.
Not sure about other shells