What are the potential pitfalls of using shell_exec() in PHP to execute shell commands?

Using shell_exec() in PHP to execute shell commands can pose security risks if not handled properly. It opens up the possibility of command injection attacks if user input is not properly sanitized. To mitigate this risk, always validate and sanitize user input before passing it to shell_exec(). Additionally, consider using escapeshellarg() or escapeshellcmd() to escape user input before using it in shell commands.

$user_input = $_POST['user_input'];
$validated_input = escapeshellarg($user_input);
$output = shell_exec("ls " . $validated_input);
echo $output;