mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2033 (markdown)
20
SC2033.md
Normal file
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.
|
Reference in New Issue
Block a user