SC2264 – ShellCheck Wiki

See this page on GitHub

Sitemap


This function unconditionally re-invokes itself. Missing command?

Problematic code:

ls() {
  ls --color=always "$@"
}

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

Correct code:

Note that command is the literal name of a shell builtin. You should not replace it:

ls() {
  command ls --color=always "$@"
}

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

Rationale:

ShellCheck found a function that immediately and unconditionally re-invokes itself, causing infinite recursion.

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.

Exceptions:

ShellCheck does not intend to warn about infinite recursion or fork bombs in general. This warning is purely meant for unintentional bugs in well meaning wrapper functions.

If ShellCheck is triggering on an intentionally malicious fork bomb, either ignore the issue, or simply add a leading command or condition:

:() { true && :|: & }

ShellCheck is a static analysis tool for shell scripts. This page is part of its documentation.