How to mkdir and switch to new directory in one line

懵懂的女人 提交于 2019-12-01 06:30:45

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

You can define a function in bash to do this:

function mkcd() {
    mkdir $1 && eval cd $1
}

A close answer that can help. It is easy to type.

 mkdir php7 && cd !$ 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!