Dup Goto 📝

Capturing Output of Shell Commmand

PT2/lang/php/system php exec capture 06-19 22:36:53
To Pop
18 lines, 81 words, 668 chars Wednesday 2024-06-19 22:36:53

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