Skip to content

Files

Latest commit

 

History

History
37 lines (23 loc) · 883 Bytes

SC2033.md

File metadata and controls

37 lines (23 loc) · 883 Bytes

Pattern: Passing shell function to external command

Issue: -

Description

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.

Example of incorrect code:

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

Example of correct code:

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

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" "$@"
}

Further Reading