Is there a shortcut to mkdir foo and immediately cd into it?

This is something I do frequently

$ mkdir foo
$ cd foo

This works as a single command, but it's more keystrokes and saves no time.

$ mkdir foo && cd foo

Is there a shortcut for this?

Edit

With the use of help below, this seems to be the most elegant answer.

# ~/.bashrc
function mkcd {
  if [ ! -n "$1" ]; then
    echo "Enter a directory name"
  elif [ -d $1 ]; then
    echo "\`$1' already exists"
  else
    mkdir $1 && cd $1
  fi
}

I'm no Linux/bash expert, but try putting this in your .bashrc.

function mkdir
{
  command mkdir $1 && cd $1
}

PS Thanks to Dennis for using command mkdir.


The bash, zsh Shells

If you don't want another function to remember and don't mind bashisms:

$ mkdir /home/foo/doc/bar && cd $_

The $_ (dollar underscore) bash command variable contains the most recent parameter. So if a user were to type the following at the command line: echo foo bar && echo $_ baz, then the output would be as follows:

foo bar
bar baz

The fish Shell

In the fish shell, I would type the following:

> mkdir /home/foo/doc/bar
> cd alt + ↑

The alt key combined with either the up or the down arrow key will cycle through command parameter history.


For oh-my-zsh users:
$ take 'directory_name'

Reference: https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet