Created SC2033 (markdown)

koalaman
2014-02-25 17:17:27 -08:00
parent a2c48b3945
commit 94bb29219d

20
SC2033.md Normal file

@@ -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.