Take the 2-minute tour ×

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


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.

share|improve this question
add comment

10 Answers

up vote 54 down vote accepted

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.

share|improve this answer
add comment

You cannot in ksh, but you can in csh.

alias mkcd 'mkdir \!^; cd \!^1'

In ksh, function is the way to go. But if you really really wanted to use alias:

alias mkcd='_(){ mkdir $1; cd $1; }; _'
share|improve this answer
Thanks! This is neat. –  Tanuj Bhargava Jul 16 '13 at 13:38
add comment

To quote the bash man page:

There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below).

So it looks like you've answered your own question -- use a function instead of an alias

share|improve this answer
add comment

You actually can't do what you want with Bash aliases, since aliases are static. Instead, use the function you have created.

Look here for more information: http://www.mactips.org/archives/2008/01/01/increase-productivity-with-bash-aliases-and-functions/. (Yes I know it's mactips.org, but it's about Bash, so don't worry.)

share|improve this answer
The link does not work anymore. –  Utkarsh Sinha Nov 7 '12 at 5:05
add comment

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
share|improve this answer
add comment

This works in ksh:

$ alias -x mkcd="mkdir \$dirname; cd \$dirname;"
$ alias mkcd
mkcd='mkdir $dirname; cd $dirname;'
$ dirname=aaa 
$ pwd
$ mkcd
$ pwd

The "-x" option make the alias "exported" - alias is visible in subshells.

And be aware of fact that aliases defined in a script are not visible in that script (because aliases are expanded when a script is loaded, not when a line is interpreted). This can be solved with executing another script file in same shell (using dot).

share|improve this answer
add comment

I think you are able to do it with shell functions if you are using bash: http://www.cyberciti.biz/faq/linux-unix-pass-argument-to-alias-command/

share|improve this answer
add comment

I found that functions cannot be written in ~/.cshrc file .. Here in alias which takes arguments

for example, arguments passed to 'find' command

alias fl "find . -name '\!:1'"     
Ex: >fl abc

where abc is the argument passed as !:1

share|improve this answer
That's not useful for the OP, whose default shell is bash. And Sanjaya R's answer mentioned csh aliases 4 years ago. –  Keith Thompson Jun 18 '13 at 19:50
add comment

Here's a simple example function using python. You can stick in ~/.bashrc
You gotta have a space after the first left curly bracket
The python command needs to be in double quotes to get the variable substitution
Don't forget that semicolon at the end

function count(){ python -c "for num in xrange($1):print num";}

$ count 6
share|improve this answer
add comment

You may also find this command useful:

mkdir dirname && cd $_

where dirname is the name of the directory you want to create

share|improve this answer
add comment

Your Answer


