Run another command before running the command the user wants to run
Sometimes an alias isn't powerful enough to easily do what you want, so here's a way without using them.
In some file that is sourced when your shell starts (e.g. .bashrc
), add the following function:
ls () {
echo "Hello world!"
command ls "$@"
}
Unlike an alias, a function can recurse. That's why command ls
is used instead of ls
; it tells your shell to use the actual ls
instead of the function you've just defined.
You must not forget to call ls
:
alias ls='echo "Hello World!"; ls'