shell_exec returns all of the output stream as a string. exec returns the last line of the output by default, but can provide all output as an array specifed as the second parameter.

See

  • http://php.net/manual/en/function.shell-exec.php
  • http://php.net/manual/en/function.exec.php
Answer from Daniel A. White on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.shell-exec.php
PHP: shell_exec - Manual
Here is a easy way to grab STDERR and discard STDOUT: add '2>&1 1> /dev/null' to the end of your shell command For example: <?php $output = shell_exec('ls file_not_exist 2>&1 1> /dev/null'); ?>
๐ŸŒ
OnlinePHP
onlinephp.io โ€บ shell-exec โ€บ manual
shell_exec - OnlinePHP.io Example
A string containing the output from the executed command, false if the pipe cannot be established or null if an error occurs or the command produces no output.
๐ŸŒ
Eli the Computer Guy
elithecomputerguy.com โ€บ 2020 โ€บ 04 โ€บ php-send-shell-commands-with-shell_exec
PHP โ€“ Send Shell Commands with shell_exec() โ€“ Eli the Computer Guy
April 14, 2020 - <input type="text" name="website"> <input type="submit"> </form> <?php $website = $_POST['website']; $command = "ping -c 1 ".$website; echo "Website: ".$command; echo "<pre>"; echo shell_exec($command); echo "</pre>"; echo "<h4>If/Else Statement</h4>"; if (shell_exec($command)== TRUE) { echo "Website UP"; } else { echo "website DOWN"; } ?>
๐ŸŒ
BCCNsoft
doc.bccnsoft.com โ€บ docs โ€บ php-docs-7-en โ€บ function.shell-exec.html
Execute command via shell and return the complete output as a string
Example #1 A shell_exec() example ยท <?php $output = shell_exec('ls -lart'); echo "<pre>$output</pre>"; ?> Note: This function is disabled when PHP is running in safe mode. exec() - Execute an external program ยท escapeshellcmd() - Escape shell metacharacters ยท
๐ŸŒ
Linux Hint
linuxhint.com โ€บ execute_shell_command_php
Execute Shell Command in PHP using exec() โ€“ Linux Hint
<?php //Store the output of the executed command in an array exec('ls -l', $output, $status); //Print all return values of the executed command as array print_r($output); echo "<br/>"; //Print the output of the executed command in each line foreach($output as $value) { echo $value."<br />"; ...
๐ŸŒ
Linux Tip
linuxscrew.com โ€บ home โ€บ programming โ€บ php โ€บ php shell_exec function: how to use it [with examples]
PHP shell_exec Function: How to Use It [With Examples]
February 23, 2021 - $fileList = shell_exec("ls -la 2>/dev/null >/dev/null &"); echo "<pre>$fileList</pre>"; // Will be empty as the output was redirected and PHP did not wait for the response ยท Generally, using shell_exec() in production isnโ€™t a great idea as ...
Find elsewhere
๐ŸŒ
Phptutorial
phptutorial.info
shell-exec function - PHP tutorial
Example #1 A shell_exec() example ยท <?php $output = shell_exec('ls -lart'); echo "<pre>$output</pre>"; ?> Note: This function is disabled when PHP is running in safe mode. exec() - Execute an external program ยท escapeshellcmd() - Escape shell metacharacters ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-shell_exec-vs-exec-function
PHP shell_exec() vs exec() Function - GeeksforGeeks
July 11, 2025 - Note: This function is disabled when PHP is running in safe mode. Example: In this example we use the shell_exec() function to execute the ls command, listing files and directories.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 3944860 โ€บ shell-exec-php-example
multithreading - shell_exec php example - Stack Overflow
You need to run curl multi exec on your php script with a parameter and receive an answer after run, like: http://localhost/phpjob.php?par1=asdasd&par2=1212.... ... Find the answer to your question by asking.
๐ŸŒ
Anto
anto.online โ€บ home โ€บ code โ€บ how to execute shell commands via php
How to execute shell commands via PHP - Anto ./online
June 17, 2022 - Finally, letโ€™s see how the system command is used using the exec() example from above: <?php $resultCode=null; //get the memory info of a Ubuntu machine system('free', $resultCode); echo "Returned with status $resultCode and output:\n"; ... total used free shared buff/cache available Mem: 16351256 9246984 238044 307576 6866228 6531868 Swap: 2097148 285036 1812112 Returned with status 0 and output: It is easy to execute shell commands in PHP.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ php exec() function
PHP exec() Function - Scaler Topics
March 18, 2024 - The exec() function in PHP is used to execute a command in the operating system's shell or terminal. It allows PHP scripts to interact with the underlying system and run external programs or commands. The function takes the command as a parameter and returns the last line of the command's output.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ explain-the-difference-between-shell_exec-and-exec-functions
Explain the Difference Between shell_exec() and exec() Functions - GeeksforGeeks
July 23, 2025 - This function gets disabled when PHP runs in safe mode. ... Parameter: shell_exec() function passes only a single argument($cmd) as it holds the command that is to be executed.