Override a builtin command with an alias

alias, bash, built-in, function, shell

Solution

I also tried `cd(){ echo before; cd $1; echo after; }` however it repetedly echos "before".

because it calls recursively the `cd` defined by you. To fix, use the `builtin` keyword like:

cd(){ pwd; builtin cd "$@"; pwd; }

Ps: anyway, IMHO isn't the best idea redefining the shell builtins.

Problem

I am trying to make an alias that overrides the `cd` command. This is going to execute a script before and after the "real" `cd`. Here is what I have so far: `alias cd="echo before; cd $1; echo after"` This executes the `echo before` and `echo after command` however it always changes directory `~` How would I fix this? I also tried `cd(){ echo before; cd $1; echo after; }` however it repetedly echos "before".

Original source