mkdir and cd

Many times, i find myself making and moving to a directory quite a bit, normally I use something like this$ mkdir ~/dir && cd ~/dir
or$ mkdir -p create/new/dir && cd create/new/dir
You can’t have a simple BASH script that reads “mkdir $1 && cd $1” because it would only switch to the directory while running as a child process instead of actually switching to the directory.
However there is a quick and easy way to combine the tasks, add a function mkcd() to your .bashrc.$ cat >> ~/bashrc
function mkcd(){
[ -n “$1” ] && mkdir -p “$@” && cd “$1”;
}