What are potential pitfalls when using the exec function in PHP to run external processes?

One potential pitfall when using the exec function in PHP to run external processes is the risk of command injection vulnerabilities if user input is not properly sanitized. To prevent this, always validate and sanitize user input before passing it to the exec function.

$user_input = $_POST['input'];
$sanitized_input = escapeshellarg($user_input);
exec("command " . $sanitized_input);