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
Answer from Gavriel on Stack Overflow
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 :(

Discussions

What is different between exec(), shell_exec, system() and passthru() functions in PHP? - Stack Overflow
Anybody please tell me. I want know the different between the exec(), shell_exec, system() and passthru() function? I search from php.net unable to get the answers I need. 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
Php Exec() Vs Shell_exec() Vs Over System() - Webune
If You Are Familiar With Php, Have You Ever Wondered What Is The Difference Between Exec(), Shell_exec() And System() More on webune.com
🌐 webune.com
command - PHP exec() vs system() vs passthru() - Stack Overflow
Most of the programs we have been executing thus far have been, more or less, real programs1. However, the environment in which Windows and Unix users operate is actually much richer than this. Windows users have the option of using the Windows Command Prompt program, cmd.exe This program is known as a command shell. ... One fascinating function that PHP ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
PHP
php.net › manual › en › function.system.php
PHP: system - Manual
For example: system("timeconfig > /dev/tty < /dev/tty"); System will wait for the program to finish before continuing. ... another reason to use shell_exec instead of system is when the result is multiple lines such as grep or ls <?php // this correctly sets answer string to all lines found //$answer = shell_exec ("grep 'set of color names' *.php "); //echo "answer = $answer"; // this passes all lines to output (they show on page) // and sets answer string to the final line $sys = system ("grep 'set of color names' *.php "); echo "sys =(($sys))"; ?> here is view/source resulting from system ca
🌐
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.
🌐
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.
🌐
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.

🌐
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 ...
Find elsewhere
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.
🌐
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.
🌐
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.
🌐
W3Docs
w3docs.com › php
PHP exec() vs system() vs passthru()
The exec(), system(), and passthru() functions are all used to execute external programs in PHP.
🌐
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 - Knowing the differences between these functions is useful for developers who need to run external commands from their PHP scripts. The shell_exec() function returns all output from the executed command as a string. This includes standard output ...
🌐
Wikitechy
wikitechy.com › technology › php-exec-vs-system-vs-passthru
exec() vs system() vs passthru() - Wikitechy
October 30, 2018 - The system() call also tries to automatically flush the web server’s output buffer after each line of output if PHP is running as a server module. 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.
🌐
Experts Exchange
experts-exchange.com › questions › 25060086 › What-is-the-difference-between-exec-and-shell-exec.html
Solved: What is the difference between exec() and shell_exec() | Experts Exchange
December 13, 2013 - To be a bit clearer about that: if you used shell_exec to execute the command "ls -l" it would return the current directory listing, however if you executed it in system() it would return nothing, which is why system is the more efficient command.
🌐
Hacking with PHP
hackingwithphp.com › 4 › 12 › 0 › executing-external-programs
Executing external programs: exec(), passthru(), and virtual() – Hacking with PHP - Practical PHP
Note that fortune may not be installed or available to your PHP scripts - contact your system administrator to find out. There are other execution functions available, notably shell_exec() and system(), however they are largely irrelevant - shell_exec(), for example, works in exactly the same ...
🌐
KnownHost
knownhost.com › forums › knownhost shared and reseller hosting › cpanel shared hosting
php exec() or system() or even shell_exec()... | KnownHost Community Forum
May 13, 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.
🌐
Wikitechy
wikitechy.com › technology › php-shell_exec-vs-exec
php shell_exec() vs exec() - Wikitechy
October 30, 2018 - shell_exec · Description · Example ... executable in the path) echo exec('whoami'); ?> [ad type=”banner”] shell_exec — Execute command via shell and return the complete output as a string ·...