Pattern: Function unconditionally re-invokes itself.
Issue: -
This generally happens when writing a wrapper function with the same name as an existing command, but forgetting to make sure it invokes the existing command and not itself. This is what happened in both of the problematic examples.
To invoke a command when a function by the same name is defined, i.e. to suppress function lookup during execution, use the command confusingly named command
. For example, to run the system's ls
instead of the shell function ls
, use command ls
.
Example of incorrect code:
ls() {
ls --color=always "$@"
}
cd() {
cd "$@" && ls
}
Example of correct code:
ls() {
command ls --color=always "$@"
}
cd() {
command cd "$@" && ls
}