What potential security risks should be considered when using exec() function in PHP to execute shell commands?

Using the exec() function in PHP to execute shell commands can pose security risks such as command injection attacks if user input is not properly sanitized. To mitigate this risk, it is important to validate and sanitize any user input before passing it to the exec() function.

$user_input = $_POST['user_input'];
$clean_input = escapeshellarg($user_input);
exec('command ' . $clean_input);