1

Possible Duplicate:
Shell Script: How to pass command line arguments to an UNIX alias?

How do I insert arguments into a bash alias?

for example, whenever I create a new directory & I cd into it most of the time but for that I have to run two commands.

$ mkdir directory
$ cd directory

so, I was wondering if it is possible to create a new directory & switch to it in singly commands. tried to add the following alias in my .bashrc file:

alias mkdir="mkdir $@ && cd $@"

so, I could call it normally mkdir directory & it would create & then switch to that directory. But no dice, it didn't work!

Any pointers on how can I insert arguments into an alias?

Community
  • 1
  • 1
CuriousMind
  • 33,537
  • 28
  • 98
  • 137
  • [There is no way to do that](http://stackoverflow.com/questions/1821495/how-do-i-include-parameters-in-a-bash-alias). – l0b0 Apr 12 '12 at 11:11
  • Looks like this has been answered: http://stackoverflow.com/questions/941338/shell-script-how-to-pass-command-line-arguments-to-an-unix-alias – lucrussell Apr 12 '12 at 11:12

1 Answers1

4

Consider defining it as a function.

function mkdir2 {
  mkdir $@
  cd $@
}

You won't be able to alias it to mkdir in this way, but it'll do as you expect.

Felix Fung
  • 1,711
  • 19
  • 27
  • You can name the function mkdir, but you cannot call it recursively: mkdir() { command mkdir $1; cd $1; } – William Pursell Apr 12 '12 at 13:25
  • slight improvement - quoted arguments, don't try to cd if the mkdir failed: `function mkdir { command mkdir "$@" && cd "$@"; }` – Mark Reed Apr 18 '12 at 01:39