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

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.


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.


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

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