Skip to content

Files

Latest commit

 

History

History
37 lines (25 loc) · 921 Bytes

SC2264.md

File metadata and controls

37 lines (25 loc) · 921 Bytes

Pattern: Function unconditionally re-invokes itself.

Issue: -

Description

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
}

Further Reading