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 />"; ...
🌐
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 ·
Find elsewhere
🌐
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.
🌐
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 ...
🌐
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.
🌐
Position Is Everything
positioniseverything.net › home › php shell_exec: learn to run shell commands from your script
PHP Shell_exec: How To Execute Commands Through the Shell?
December 29, 2025 - The shell syntax looks like this: shell_exec(command). However, you’ll get NULL when either the command returns nothing or an error occurs. Moreover, false and a warning are returned on the failed establishment of the pipe. You need to enable the PHP shell_exec() function in the php.ini file before using it in your program.
🌐
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.