They have slightly different purposes.

  • exec() is for calling a system command, and perhaps dealing with the output yourself.
  • system() is for executing a system command and immediately displaying the output - presumably text.
  • passthru() is for executing a system command which you wish the raw return from - presumably something binary.

Regardless, I suggest you not use any of them. They all produce highly unportable code.

Answer from Kalium on Stack Overflow
🌐
PHP
php.net › manual › en › function.system.php
PHP: system - Manual
Finally, I found a comment in a blog by a certain amazing guy that solved my problems. Adding the string ' 2>&1' to the command name finally returned the output!! This works in exec() as well as system() in PHP since it uses stream redirection to redirect the output to the correct place!
Top answer
1 of 5
220

They have slightly different purposes.

  • exec() is for calling a system command, and perhaps dealing with the output yourself.
  • system() is for executing a system command and immediately displaying the output - presumably text.
  • passthru() is for executing a system command which you wish the raw return from - presumably something binary.

Regardless, I suggest you not use any of them. They all produce highly unportable code.

2 of 5
173

The previous answers seem all to be a little confusing or incomplete, so here is a table of the differences...

+----------------+-----------------+----------------+----------------+
|    Command     | Displays Output | Can Get Output | Gets Exit Code |
+----------------+-----------------+----------------+----------------+
| system()       | Yes (as text)   | Last line only | Yes            |
| passthru()     | Yes (raw)       | No             | Yes            |
| exec()         | No              | Yes (array)    | Yes            |
| shell_exec()   | No              | Yes (string)   | No             |
| backticks (``) | No              | Yes (string)   | No             |
+----------------+-----------------+----------------+----------------+
  • "Displays Output" means it streams the output to the browser (or command line output if running from a command line).
  • "Can Get Output" means you can get the output of the command and assign it to a PHP variable.
  • The "exit code" is a special value returned by the command (also called the "return status"). Zero usually means it was successful, other values are usually error codes.

Other misc things to be aware of:

  • The shell_exec() and the backticks operator do the same thing.
  • There are also proc_open() and popen() which allow you to interactively read/write streams with an executing command.
  • Add "2>&1" to the command string if you also want to capture/display error messages.
  • Use escapeshellcmd() to escape command arguments that may contain problem characters.
  • If passing an $output variable to exec() to store the output, if $output isn't empty, it will append the new output to it. So you may need to unset($output) first.
Discussions

shell - What are the differences of system(), exec() and shell_exec() in PHP? - Stack Overflow
Takes only 1 arg -- the command · $plain_text = shell_exec('pwd;ls -alh'); echo $plain_text; /* /var/www/vhosts/example.com/httpdocs/dir/subdir total 20K drwxr-xr-x 3 root root 4.0K Feb 7 03:17 . drwxr-xr-x 7 ubuntu psacln 4.0K Feb 6 21:46 .. -rw-r--r-- 1 ubuntu psacln 550 Feb 6 22:45 .htaccess drwxr-xr-x 2 ubuntu psacln 4.0K Feb 7 00:22 cache -rw-r--r-- 1 ubuntu psacln 1.2K Feb 7 03:43 index.php */ Echos single or multiline plain text. No need for echo system... More on stackoverflow.com
🌐 stackoverflow.com
Running system command from php
I am trying to put together a page that displays different information about the system. One of the commands I want to run is hdparm so that I can gather the hard drive serial number. My code looks like this: $hd = system("hdparm -I /dev/sda"); echo $hd; But I get nothing when I pull up the... More on tek-tips.com
🌐 tek-tips.com
25
0
May 21, 2010
Executing unix shell commands using PHP - Stack Overflow
Oh, and running arbitrary commands from your users is perhaps one of the greatest security risks that you could ever conceive of, but I kind of assume you know this by now. ... But like sdleihssirhc mentioned, watchout this IS very dangerous and you should NOT allow everything to be executed! If you still want to do it, to get the output of the shell, just use · exec The output of the shell will be passed in the second parameter. ... See http://php.net/system ... More on stackoverflow.com
🌐 stackoverflow.com
Executing a shell command from PHP with shell_exec - Unix & Linux Stack Exchange
First you need to permit your Webserver to Execute System commands. However in my instants I will be using CentOS6.3 64bit LAMP server. First of all we need to know our current user that our apache uses, in most case its apache, lets check the the user name by PHP to make sure our username. More on unix.stackexchange.com
🌐 unix.stackexchange.com
June 19, 2011
People also ask

How do malicious hackers use web shells?
Malicious hackers use web shells to take control of an already compromised server. First, they exploit a vulnerability in your website or web application such as SQL injection, remote code execution, or others. Then, they upload a web shell to your web server. From now on, they can run any commands that they like on your server. See a step-by-step example of an attack that leads to full server compromise.
🌐
acunetix.com
acunetix.com › blog › articles › web-shells-101-using-php-introduction-web-shells-part-2
Web Shells 101 Using PHP (Web Shells Part 2) | Acunetix
How can I detect web shells?
You can detect web shells by log analysis. However, you should not focus on detecting web shells but instead, you should detect vulnerabilities that can let attackers take control of your server. Even if you detect a web shell, that will not stop attackers from taking over control again if the vulnerabilities are still there. To detect web vulnerabilities and learn how to eliminate them, use Acunetix. See what Acunetix Premium can do for you.
🌐
acunetix.com
acunetix.com › blog › articles › web-shells-101-using-php-introduction-web-shells-part-2
Web Shells 101 Using PHP (Web Shells Part 2) | Acunetix
What is a web shell?
A web shell is a small application that an attacker runs on your web server. They can then use this application to remotely access your server and run commands on it. A web shell by itself is never an attack, it is the aftermath of a successful attack on your website or web application. This means that if you have a web shell, you have a much more serious problem to worry about. See how a web shell works in practice.
🌐
acunetix.com
acunetix.com › blog › articles › web-shells-101-using-php-introduction-web-shells-part-2
Web Shells 101 Using PHP (Web Shells Part 2) | Acunetix
🌐
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › function.system.html
Execute an external program and display the output
<?php echo '<pre>'; // Outputs all the result of shellcommand "ls", and returns // the last output line into $last_line. Stores the return value // of the shell command in $retval. $last_line = system('ls', $retval); // Printing additional info echo ' </pre> <hr />Last line of the output: ' .
🌐
Acunetix
acunetix.com › blog › articles › web-shells-101-using-php-introduction-web-shells-part-2
Web Shells 101 Using PHP (Web Shells Part 2) | Acunetix
March 5, 2025 - The following example on a Microsoft Windows machine will run the dir command to return a directory listing of the directory in which the PHP file is executed. <?php // Return the listing of the directory where the file runs (Windows) system("dir"); ?> --> Volume in drive C has no label.
🌐
Tutorialspoint
tutorialspoint.com › php › php_system_calls.htm
PHP System Calls
system(string $command, int &$result_code = null): string|false · The system() call tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.
Find elsewhere
🌐
GitHub
github.com › pear › System_Command › blob › master › System › Command.php
System_Command/System/Command.php at master · pear/System_Command
PHP commandline execution interface. Contribute to pear/System_Command development by creating an account on GitHub.
Author   pear
🌐
W3Docs
w3docs.com › php
PHP exec() vs system() vs passthru()
They differ in how they handle the output of the program: exec() executes the command and returns the last line of output as a string, or FALSE on error. system() executes the command and prints the output directly to the output buffer.
🌐
Zyxware
zyxware.com › articles › 5185 › how-to-run-system-commands-using-php
How to run system commands using php? | Zyxware
June 28, 2016 - <?php $deletefiles = 'rm -rf {folder name}/*'; shell_exec($deletefiles); ?> You can execute any system commands using shell_exec like the above example.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-shell_exec-vs-exec-function
PHP shell_exec() vs exec() Function - GeeksforGeeks
July 11, 2025 - While shell_exec() returns the entire output of the command as a string, exec() only returns the last line, offering more control over output handling. The shell_exec() function in PHP executes a command via the shell and returns the complete output as a string.
Top answer
1 of 2
93

exec — Execute an external program

system — Execute an external program and display the output

shell_exec — Execute command via shell and return the complete output as a string

so if you don't need the output, I would go with exec.

Further details:

  • http://php.net/manual/en/function.exec.php
  • http://php.net/manual/en/function.system.php
  • http://php.net/shell_exec
2 of 2
0

Just expanding on the existing answer with examples:

All functions will attempt to execute the command almost as if from the terminal. The main difference is in the output and error handling:

exec()

Outputs to var only the last line; Outputs array with each line to 2nd arg; Outputs result_code to 3rd arg -- any non-zero result_code is an error. Here's a list of result_codes

$last_line = exec('pwd;ls -alh', $all_lines_array, $result_code);
echo "LAST_LINE: $last_line";
print_r($all_lines_array);
echo "RESULT_CODE: $result_code";

/*
LAST_LINE: -rw-r--r-- 1 ubuntu psacln 1.2K Feb  7 03:43 index.php 

Array
(
    [0] => /var/www/vhosts/example.com/httpdocs/dir/subdir
    [1] => total 20K
    [2] => drwxr-xr-x 3 root         root   4.0K Feb  7 03:17 .
    [3] => drwxr-xr-x 7 ubuntu psacln 4.0K Feb  6 21:46 ..
    [4] => -rw-r--r-- 1 ubuntu psacln  550 Feb  6 22:45 .htaccess
    [5] => drwxr-xr-x 2 ubuntu psacln 4.0K Feb  7 00:22 cache
    [6] => -rw-r--r-- 1 ubuntu psacln 1.2K Feb  7 03:43 index.php
)

RESULT_CODE: 0
*/

shell_exec()

Outputs single or multiline plain text to var. Takes only 1 arg -- the command

$plain_text = shell_exec('pwd;ls -alh');
echo $plain_text;

/*
/var/www/vhosts/example.com/httpdocs/dir/subdir
total 20K
drwxr-xr-x 3 root         root   4.0K Feb  7 03:17 .
drwxr-xr-x 7 ubuntu psacln 4.0K Feb  6 21:46 ..
-rw-r--r-- 1 ubuntu psacln  550 Feb  6 22:45 .htaccess
drwxr-xr-x 2 ubuntu psacln 4.0K Feb  7 00:22 cache
-rw-r--r-- 1 ubuntu psacln 1.2K Feb  7 03:43 index.php
*/

system()

Echos single or multiline plain text. No need for echo system();. result_code is the same as exec()

system('pwd;ls -alh',$result_code);
echo "RESULT_CODE: $result_code";
/*
/var/www/vhosts/example.com/httpdocs/dir/subdir
total 20K
drwxr-xr-x 3 root         root   4.0K Feb  7 03:17 .
drwxr-xr-x 7 ubuntu psacln 4.0K Feb  6 21:46 ..
-rw-r--r-- 1 ubuntu psacln  550 Feb  6 22:45 .htaccess
drwxr-xr-x 2 ubuntu psacln 4.0K Feb  7 00:22 cache
-rw-r--r-- 1 ubuntu psacln 1.2K Feb  7 03:43 index.php

RESULT_CODE: 0
*/

Backtick quotes ``

Basically, alias for shell_exec(). Outputs single or multiline plain text to var

$plain_text = `pwd;ls -al`;
echo $plain_text;

/*
/var/www/vhosts/example.com/httpdocs/dir/subdir
total 20K
drwxr-xr-x 3 root         root   4.0K Feb  7 03:17 .
drwxr-xr-x 7 ubuntu psacln 4.0K Feb  6 21:46 ..
-rw-r--r-- 1 ubuntu psacln  550 Feb  6 22:45 .htaccess
drwxr-xr-x 2 ubuntu psacln 4.0K Feb  7 00:22 cache
-rw-r--r-- 1 ubuntu psacln 1.2K Feb  7 03:43 index.php
*/

exec() appears to be most versatile as you can iterate over the array of lines, get the last line only, and the result_code.

shell_exec() and backticks `` are great if plain text works better for you.

system() echos automatically :(

🌐
Linux Hint
linuxhint.com › execute_shell_command_php
Execute Shell Command in PHP using exec() – Linux Hint
How this function can be used in PHP are shown in this tutorial. string exec ( string $command [, array &$output [, int &$return_var ]] ) This function can take three arguments. The first argument is mandatory that will take the system command. The other two arguments are optional.
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › web development › php
Running system command from php | Tek-Tips
May 21, 2010 - I am trying to put together a page that displays different information about the system. One of the commands I want to run is hdparm so that I can gather the hard drive serial number. My code looks like this: $hd = system("hdparm -I /dev/sda"); echo $hd; But I get nothing when I pull up the...
🌐
ExeOutput for PHP
exeoutput.com › home › exeoutput for php help › using exec(), system() in applications
Using exec(), system() in Applications - ExeOutput for PHP
September 20, 2025 - The following PHP code illustrates how to use these commands to run external EXE files, BAT (batch) files, and more. By default, a console window is displayed. To run a batch file located in the same folder as the application’s EXE file: In this sample, the exo_getglobalvariable PHP function returns the path to the folder where the EXE is located. <?php $mypath = exo_getglobalvariable('HEPublicationPath', '') . 'sample.bat'; echo exec($mypath); ?> ... <?php echo system('echo | C:\\WINDOWS\\System32\\wbem\\wmic.exe path win32_computersystemproduct get uuid'); ?>
🌐
Anto
anto.online › home › code › how to execute shell commands via php
How to execute shell commands via PHP - Anto ./online
June 17, 2022 - The PHP functions to execute shell command are: shell_exec(), exec() or system(). These functions are remarkably similar but have slight differences.