How to mkdir and switch to new directory in one line

The portable way to do this is with a shell function--not a bash function (using bashims like function). Put this in the relevant .profile for interactive use:

 mkdir () {
    case $1 in
       (-c) command mkdir -p "$2" && cd "$2";;
       (*)  command mkdir "$@";;
    esac
 }

This adds the -c option to mkdir for interactive use. Without -c the utility acts as it always does.- And note the quoting of "$2" so this works with directories with white space in their name.


nothing prevents you from creating your own alias or small script

mkdir $1 && cd $1

Or you can use ';' to separate commands, like:

mkdir php5.3 ; cd php5.3