What are the best practices for securely executing external scripts from a PHP application?

When executing external scripts from a PHP application, it is important to follow best practices to ensure security. One way to do this is by using functions like `escapeshellarg()` to escape any user input and prevent command injection attacks. Additionally, limiting the permissions of the script being executed and validating input data can help reduce the risk of vulnerabilities.

$script = '/path/to/external/script.sh';
$input = $_POST['input'];

$escaped_input = escapeshellarg($input);
$output = shell_exec("$script $escaped_input");

echo $output;