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
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
-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() ... More on stackoverflow.com
🌐 stackoverflow.com
Replace exec, shell_exec and passthru calls with Symfony Process component
There are multiple places where php internal shell execution is used and each has its own approach. I propose using Process component to essentially replace all of them with proc_open(), as the whole class is up to date, regularly updated, more secure and cross-platform compatible. The Process class executes a command in a sub-process, taking care of the differences between operating system and escaping arguments to prevent security issues. It replaces PHP functions like exec, passthru... More on github.com
🌐 github.com
1
November 24, 2021
shell - PHP shell_exec() vs exec() - Stack Overflow
Is shell_exec() just a shorthand for exec()? It seems to be the same thing with fewer parameters. ... There are also other functions: system(), passthru()… More on stackoverflow.com
🌐 stackoverflow.com
PHP's passthru() vs shell_exec() - Stack Overflow
The function passthru() is good when the output contains binary data as the function outputs the raw output. However, the documentation for the function shell_exec() does not say whether the data More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/phphelp › shell_exec() exec() passthru() system() - which??
r/PHPhelp on Reddit: shell_exec() exec() passthru() system() - Which??
March 24, 2015 -

Hey all,

I am trying to build something at the moment, which so far is going very well! However what I am trying to achieve is a way to guarantee the response from a shell_exec() call to be echoed out in PHP.

To explain a little, I am building a solution where I can add an abstraction layer for security purposes on top of any command line calls you may require. This is achieved by having a command object which will interface with a "library" object and invoke pre-defined methods which will of course feed you back the output of said command to do with as you please.

An example of what I am doing, using sox to turn a stereo audio track into a mono track :

$command = new Command(); $command->call("sox")->addSub("mono", "track.wav")->execute(); if($command->response != "") { // Do action }

What this does is load in the "sox" library as an object and invokes the "mono" method on the library object. Using the argument "track.wav". This way I can name my most used commands to something memorable instead of having to use the following shell_exec("sox track.wav -c 1 track.wav")

This is just a simplified version of what you can do. However I am having issues with certain libraries and echoing out the response, so am looking for a way to guarantee this.

🌐
Kavoir
kavoir.com › 2009 › 06 › php-differences-between-exec-system-and-passthru.html
PHP: Differences between exec(), shell_exec(), system() and passthru() – Kavoir LLC
June 2, 2009 - All 3 PHP functions: exec(), system() and passthru() executes an external command, but the differences are: exec(): returns the last line of output from the command and flushes nothing. shell_exec(): returns the entire output from the command and flushes nothing.
🌐
PHP
php.net › manual › en › function.system.php
PHP: system - Manual
Use passthru in this case, it appears to work as intended. ... If no headers have been printed, calling the system() function will flush the headers to the client and insert a newline; no further headers or cookies will be transferred to the browser. In version 3.0.7, you will not be warned that the Header() function failed, but will be warned if trying to set a cookie. If you want to continue to send headers after the function call, use exec() instead.
🌐
W3Docs
w3docs.com › php
PHP exec() vs system() vs passthru()
passthru() executes the command and prints the raw output directly to the output buffer. It returns the exit status of the command as an integer, or FALSE on error. It's important to note that all of these functions can be vulnerable to command ...
🌐
Hacking with PHP
hackingwithphp.com › 4 › 12 › 0 › executing-external-programs
Executing external programs: exec(), passthru(), and virtual() – Hacking with PHP - Practical PHP
The passthru() function, on the other hand, runs the program specified and prints out all the output that program generates. Calling exec() is usually preferred when the output of your program is irrelevant, whereas passthru() automatically prints your output.
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 :(

Find elsewhere
🌐
GitHub
github.com › yiisoft › yii2 › issues › 19044
Replace exec, shell_exec and passthru calls with Symfony Process component · Issue #19044 · yiisoft/yii2
November 24, 2021 - There are multiple places where php internal shell execution is used and each has its own approach. I propose using Process component to essentially replace all of them with proc_open(), as the whole class is up to date, regularly updated, more secure and cross-platform compatible. The Process class executes a command in a sub-process, taking care of the differences between operating system and escaping arguments to prevent security issues. It replaces PHP functions like exec, passthru, shell_exec and system
Author   Deele
🌐
Jeff Schaefer
schaefersoftware.com › exec-passthru-shell_exec-system
What's the difference between exec(), passthru(), shell_exec(), and system()? - Jeff Schaefer
April 25, 2016 - This function will pass the raw output along and display error messages along with output from echo. If you are dealing with a remote php file and its not working, this will usually tell you why. shell_exec() is your standard execution function.
🌐
Wikitechy
wikitechy.com › technology › php-exec-vs-system-vs-passthru
exec() vs system() vs passthru() - Wikitechy
October 30, 2018 - If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function. ... <?php echo '<pre>'; // Outputs all the result of shellcommand "ls", and returns // the last output line into $last_line.
🌐
Blogger
kevindaus.blogspot.com › 2014 › 08 › php-exec-vs-system-vs-passthru.html
Web Development Stuff / Experience - from KevinDaus: PHP - exec() vs system() vs passthru()
August 5, 2014 - exec() - "shell_exec" stop copying my feature... *works the same as shell_exec · system() - I will run your command but I will only return the last line . Please use me if you only want a one liner output · passthru() - I will run your command and proceed immediately with the next line of code.
🌐
PHPBuilder
board.phpbuilder.com › d › 10260187-using-system-passthru-exec-shell-execpassthruexecshell-exec
Using system(), passthru(), exec(), shell_exec passthru() exec() shell_exec() - PHPBuilder Forums
November 14, 2003 - I did some reading on these things but still confused until now.What is the difference between these four? I hope someone can give a simple and direct explan...