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"
}
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"
}
(((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. ;)