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
174

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.
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.system.php
PHP: system - Manual
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.
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
shell_exec() exec() passthru() system() - Which??
How about using https://packagist.org/packages/symfony/process ? More on reddit.com
๐ŸŒ r/PHPhelp
9
4
March 24, 2015
What is different between exec(), shell_exec, system() and passthru() functions in PHP? - Stack Overflow
system return false when there was an error. Does this mean when the exit code is !== 0 it will return false? So I do not have to actually check the $retval if I just want to check of the command suceeded? 2021-01-22T09:53:35.793Z+00:00 ... passthru is used for returning binary data instead of ascii. A typical example is where an image manipulation program is returning an image instead of text data. See PHP - exec... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
๐ŸŒ
W3Docs
w3docs.com โ€บ php
PHP exec() vs system() vs passthru()
system() executes the command and prints the output directly to the output buffer.
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 :(

๐ŸŒ
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 - In this article, we will learn about the shell_exec() & exec() functions in PHP. As we know that in order to execute a command in a system, we need the shell of the respective operating systems, but if we need to execute the same command with the help of a programming language like PHP, then we use these two functions.
๐ŸŒ
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.

Find elsewhere
๐ŸŒ
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 - shell_exec(): returns the entire output from the command and flushes nothing. system(): returns the last line of output from the command and tries to flush the output buffer after each line of the output as it goes. passthru(): returns nothing and passes the resulting output without interference ...
๐ŸŒ
Wikitechy
wikitechy.com โ€บ technology โ€บ php-exec-vs-system-vs-passthru
exec() vs system() vs passthru() - Wikitechy
October 30, 2018 - This function, like the others, executes the program you tell it to. However, it then proceeds to immediately send the raw output from this program to the output stream with which PHP is currently working (i.e. either HTTP in a web server scenario, or the shell in a command line version of PHP). ... Calling Octave from PHP using either systemexec function in PHP and passthru?exec() and passthru() php executingexec() or passthru()How can I prevent SQL injection in PHP?How do I check if a string contains a specific word in PHP?How does PHP 'foreach' actually work?passthru() not working (possible permissions issue)PHP command not executed system()Reference - What does this symbol mean in PHP?shell_execsystem()system() and passthru() functions in PHP?What is different between exec()windows - php exec()
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-shell_exec-vs-exec-function
PHP shell_exec() vs exec() Function - GeeksforGeeks
July 11, 2025 - The shell_exec() function in PHP executes a command via the shell and returns the complete output as a string. It allows running shell commands directly from a PHP script, capturing the full output, which can be useful for automating system tasks.
๐ŸŒ
Hacking with PHP
hackingwithphp.com โ€บ 4 โ€บ 12 โ€บ 0 โ€บ executing-external-programs
Executing external programs: exec(), passthru(), and virtual() โ€“ Hacking with PHP - Practical PHP
Unlike exec() and system(), virtual() performs a virtual request to the web server for a file, almost as if your script were a client itself. This request is processed as per usual and its output is sent back to your script. Using this method you can, for example, execute a Perl script from your PHP script, or, for real weirdness, execute another PHP script from your PHP script.
๐ŸŒ
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 - For running basic commands, this is your function. It can also be run shorthand using the backtick operator. system() will echo the result of the command rather than passing it in the return value.
๐ŸŒ
Uptimia
uptimia.com โ€บ home โ€บ questions โ€บ what is the difference between shell_exec() and exec() in php?
What Is The Difference Between Shell_exec() And Exec() In PHP?
November 17, 2024 - The main difference in output format is that shell_exec() gives you a string with all output, while exec() allows you to choose between getting the last line or the full output as an array.
๐ŸŒ
Webune
webune.com โ€บ forums โ€บ php-exec-vs-shell-exec-vs-over-system.html
Php Exec() Vs Shell_exec() Vs Over System() - Webune
] system รขโ‚ฌโ€ Execute an external program and display the output system() is just like the C version of the function in that it executes the given command and outputs the result. The system() call also tries to automatically flush the web server's output buffer after each line of output ...
๐ŸŒ
KnownHost
knownhost.com โ€บ forums โ€บ knownhost shared and reseller hosting โ€บ cpanel shared hosting
php exec() or system() or even shell_exec()... | KnownHost Community Forum
May 11, 2010 - Quick reply to self since I can't edit my last message: My php works just lovely in cron without me doing any sort of changes. I'm adverse to not being able to test on a system before tossing in cron, but I'll deal. My form logic was that I'd call the same php file, and if an action was desired, exec/system/shell_exec the php script to handle it.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ php โ€บ php_system_calls.htm
PHP System Calls
PHP's library of built-in function ... the PHP code. In this chapter, we shall discuss the PHP functions used to perform system calls. The system() function is similar to the system() function in C that it executes the given command and outputs the result....