Command injection is an attack where malicious commands are executed on a system by passing unsafe user input to a shell. It occurs when an app fails to validate input before using it in system commands. This allows attackers to inject arbitrary commands that get run with the app's privileges. To prevent it, apps should strictly validate input against a whitelist and use command APIs instead of passing strings to interpreters supporting redirection.