From 94bb29219d6205e96606097c4bea185a3db03445 Mon Sep 17 00:00:00 2001 From: koalaman Date: Tue, 25 Feb 2014 17:17:27 -0800 Subject: [PATCH] Created SC2033 (markdown) --- SC2033.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 SC2033.md diff --git a/SC2033.md b/SC2033.md new file mode 100644 index 0000000..fd881ec --- /dev/null +++ b/SC2033.md @@ -0,0 +1,20 @@ +# 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. \ No newline at end of file