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?

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You can name the function mkdir, but you cannot call it recursively: mkdir() { command mkdir $1; cd $1; }
slight improvement - quoted arguments, don't try to cd if the mkdir failed: function mkdir { command mkdir "$@" && cd "$@"; }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.