Dup Ver Goto 📝

Capturing Output of Shell Commmand

PT2/lang/php/system php exec capture does not exist
To
18 lines, 81 words, 668 chars Page 'Exec' does not exist.

To execute a system command:

$output = shell_exec("command args..."); // $output is string
exec("command",$output); // $output is array of lines

To get the effect of Pythonssystem.run` where we present the command as an array, we need to use

$args = ["echo","hello","world mr","flibble"];
$escaped_args = array_map(function($x) { return escapeshellarg($x); }, $args);
$command = implode(" ",$escaped_args);
$result = system($command);

Note that the PATH is a basic one with /bin:/usr/bin:/usr/local/bin and little else.

Resources