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
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
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 :(
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
Here are the differences. Note the newlines at the end.
> shell_exec('date')
string(29) "Wed Mar 6 14:18:08 PST 2013\n"
> exec('date')
string(28) "Wed Mar 6 14:18:12 PST 2013"
> shell_exec('whoami')
string(9) "mark\n"
> exec('whoami')
string(8) "mark"
> shell_exec('ifconfig')
string(1244) "eth0 Link encap:Ethernet HWaddr 10:bf:44:44:22:33 \n inet addr:192.168.0.90 Bcast:192.168.0.255 Mask:255.255.255.0\n inet6 addr: fe80::12bf:ffff:eeee:2222/64 Scope:Link\n UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\n RX packets:16264200 errors:0 dropped:1 overruns:0 frame:0\n TX packets:7205647 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:1000 \n RX bytes:13151177627 (13.1 GB) TX bytes:2779457335 (2.7 GB)\n"...
> exec('ifconfig')
string(0) ""
Note that use of the backtick operator is identical to shell_exec().
Update: I really should explain that last one. Looking at this answer years later even I don't know why that came out blank! Daniel explains it above -- it's because exec only returns the last line, and ifconfig's last line happens to be blank.
What is different between exec(), shell_exec, system() and passthru() functions in PHP? - Stack Overflow
Php Exec() Vs Shell_exec() Vs Over System() - Webune
shell_exec() exec() passthru() system() - Which??
command - PHP exec() vs system() vs passthru() - Stack Overflow
execonly returns the last line of the generated output.shell_execreturns the full output of the command, when the command finished running.systemimmediately shows all output, and is used to show text.passthrualso returns output immediately, but is used for binary data.passthrudisplays raw data.
With both exec and shell_exec it is possible to handle the output yourself, while system and passthru won't let you customize it and immediately display the output.
A more detailed comparison can be found here.
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() vs system() vs passthru() for more info
Also see php shell_exec() vs exec().
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.
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.
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.
One solution can be:
<?php
\system('bash -c "here your command"');
or
<?php
\system('zsh -c "here your command"');
What's the point? If you use system to start another shell script, that shell script itself can decide (via its #! line) which shell it is supposed to be processed by. Aside from this, you can always explicitly call the shell. For instance, if you want to take advantage of globbing operators specific to zsh or specific zsh builtins, you can do a
# Produce list of files including hidden files. Use
# the zsh-builtin command instead of /usr/bin/echo :
system('zsh -c "echo *(DN)"')