Apple - Can I modify a terminal command to do additional stuff?

I'd tend to make a new command for this. I think it would even be logical to combine them into a single one.

go() {
    if [ -d "$1" ]; then
        cd "$1" && ls
    else
        mkdir -p "$1" && echo "Created directory $1" && cd "$1"
    fi
}

I have tried adding things like these to my .bashrc:

cd() {
    command cd "$@"
    command ls
}

mkdir() {
    command mkdir "$@"
    command cd "$@"
}

However, I've found that this can mess up scripts that use the overridden commands, and the option handling can be fragile (for example, if you want to pass -p to the above mkdir command, it's also passed to cd). Better would be just to define aliases with different names (say, c or mcd).


I think functions are the way to go. Something like

chglist() {
    cd "$1" && ls
}

as an example.