I've probably tooted about this before, but I don't know why this isn't standard.

It's just so obvious, at least to me. ;)

~ $ type mcd mcd is a function mcd () { [[ -n $1 ]] && mkdir "$1" && cd "$1" }

#shell #UnixShell #ShellScripting #sh #bash #ksh #mksh

@rl_dane I don't get it

@joel @rl_dane

I think it makes a new directory and then changes the current working directory to be the new directory

@pete @joel @rl_dane first it checks if it exists, if I remember my bash.

@joel

(((Ok, I'm going to ELI5 this for you (gladly), but you really gotta read it all carefully, ok?)))

Very simple:

function mcd() { [[ -n $1 ]] && mkdir "$1" && cd "$1" }

First of all, you've got the function declaration itself:

function mcd() { ... }

Then the guts:

[[ -n $1 ]] && mkdir "$1" && cd "$1"

The && are a logical and, basically "do the next thing if the previous thing completed successfully. So you could read it as:

if [[ -n $1 ]]; then if mkdir "$1"; then cd "$1" fi fi

The [[ -n $1 ]] basically means "return true if variable $1 (first argument) isn't blank."

And you already know what mkdir and cd do. ;)

@sotolf @pete @hyde