Skip to content
Vidar Holen edited this page Nov 15, 2021 · 8 revisions

Shell functions can't be passed to external commands. Use separate script or sh -c.

Problematic code:

foo() { bar --baz "$@"; frob --baz "$@"; };
find . -exec foo {} +

Correct code:

find . -exec sh -c 'bar --baz "$@"; frob --baz "$@";' -- {} +

Rationale:

Shell functions are only known to the shell. External commands like find, xargs, su and sudo do not recognize shell functions.

Instead, the function contents can be executed in a shell, either through sh -c or by creating a separate shell script as an executable file.

Exceptions

If you're intentionally passing a word that happens to have the same name as a declared function, you can quote it to make shellcheck ignore it, e.g.

nobody() {
  sudo -u "nobody" "$@"
}

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally