What are common pitfalls when trying to execute shell scripts from PHP?

Common pitfalls when trying to execute shell scripts from PHP include not properly escaping shell arguments, not checking for errors or handling output, and not setting the correct permissions on the script file. To avoid these pitfalls, always use escapeshellarg() to escape arguments, check for errors using the return value of shell_exec(), and ensure the script file has the appropriate permissions.

$script = '/path/to/script.sh';
$arg1 = 'argument1';
$arg2 = 'argument2';

$escaped_arg1 = escapeshellarg($arg1);
$escaped_arg2 = escapeshellarg($arg2);

$output = shell_exec("sh $script $escaped_arg1 $escaped_arg2");

if ($output === null) {
    echo "Error executing script.";
} else {
    echo $output;
}