Skip to content
koalaman edited this page Feb 26, 2014 · 8 revisions

Shell functions can't be passed to external commands.

Problematic code:

foo() { rm /run/foo; bar --baz; };
sudo foo

Correct code:

sudo sh -c 'rm /run/foo; bar --baz;'

Rationale:

Shell functions are only known to the shell. External commands like 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.

Contraindications

None.

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